算法学习——LeetCode力扣链表篇2

算法学习——LeetCode力扣链表篇2

算法学习——LeetCode力扣链表篇2_第1张图片

24. 两两交换链表中的节点

24. 两两交换链表中的节点 - 力扣(LeetCode)

描述

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

示例

示例 1:

算法学习——LeetCode力扣链表篇2_第2张图片

输入:head = [1,2,3,4]
输出:[2,1,4,3]

示例 2:

输入:head = []
输出:[]

示例 3:

输入:head = [1]
输出:[1]

提示

链表中节点的数目在范围 [0, 100] 内
0 <= Node.val <= 100

代码解析

自己写版本

原理为两个交换,交换参数为pre和cur。
交换成功后,连接pre的前一个pre2和cur的后一个aft。

#include 
#include 
#include 

using namespace std;




struct ListNode {
  int val;
  ListNode *next;
  ListNode() : val(0), next(nullptr) {}
  ListNode(int x) : val(x), next(nullptr) {}
  ListNode(int x, ListNode *next) : val(x), next(next) {}
};
 

class Solution {
public:
   	ListNode* swapPairs(ListNode* head) {
        ListNode* pre, * cur, *temp ,*aft,*pre2,*head_test;
		//链表长度为0或者为1
        if (head == nullptr || head->next == nullptr)return head;

        ListNode test(0,nullptr);
        head_test = head->next;
        pre = head;
        pre2 = &test;
        cur = pre->next;
       
       //链表长度大于2以上
        while (cur)
        {
           
            aft = cur->next;
            if (pre != nullptr|| cur != nullptr)
            {
                temp = cur->next;
                cur->next = pre;
                pre->next = temp;
                pre2->next = cur;
            }
            else break;
            pre2 = pre;
            pre = aft;
            
            if (pre != nullptr)cur = pre->next;
            else break;
        }
        return head_test;
    }
};

int main()
{
    vector<int> head = { 1,2};

    ListNode* head_test = new ListNode(0);
    ListNode* test  , *cur = head_test;
    Solution  a;

    for (int i = 0; i < head.size(); i++)
    {
       
       ListNode* temp = new ListNode(head[i]);
       cur->next = temp;
       cur = cur->next;
      
    }
    cur->next = nullptr;

    cur = head_test;
    cout << "cur list" << endl;
    while (cur->next != nullptr)
    {
        cout << cur->val << ' ';
        cur = cur->next;
    }
    cout << cur->val << endl;


    test = a.swapPairs(head_test->next);

    while (test->next != nullptr)
    {
        cout << test->val << ' ';
        test = test->next;
    }
    cout << test->val << ' ';
	return 0;

}

卡尔版本
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        if(head == nullptr || head->next == nullptr) return head;
        ListNode* headReal = new ListNode(0);
        headReal->next = head;
        
        ListNode* tmpPre = headReal;
        ListNode* tmp1 = tmpPre->next;
        ListNode* tmp2 = tmpPre->next->next;
        ListNode* tmpNext = tmpPre->next->next->next;

        while(tmpPre != nullptr && tmp1 != nullptr && tmp2 != nullptr  )
        {
            tmpPre->next = tmp2;
            tmp2->next = tmp1;
            tmp1->next = tmpNext;

            tmpPre = tmpPre->next->next;
            tmp1 = tmpPre->next;
            if( tmp1 == nullptr) break;
            tmp2 = tmpPre->next->next;
            if( tmp2 == nullptr) break;
            tmpNext = tmpPre->next->next->next;

        }


        return headReal->next;
    }
};

19. 删除链表的倒数第 N 个结点

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

描述

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

示例

示例 1:

算法学习——LeetCode力扣链表篇2_第3张图片

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]

示例 2:

输入:head = [1], n = 1
输出:[]

示例 3:

输入:head = [1,2], n = 1
输出:[1]

提示

  1. 链表中结点的数目为 sz
  2. 1 <= sz <= 30
  3. 0 <= Node.val <= 100
  4. 1 <= n <= sz

进阶:你能尝试使用一趟扫描实现吗?

代码解析

自己写暴力版本
#include 
#include 
#include 

using namespace std;

 struct ListNode {
   int val;
   ListNode *next;
   ListNode() : val(0), next(nullptr) {}
   ListNode(int x) : val(x), next(nullptr) {}
   ListNode(int x, ListNode *next) : val(x), next(next) {}
 };
 
  class Solution {
  public:
      ListNode* removeNthFromEnd(ListNode* head, int n) {
          int length = 0;
          ListNode* temp1 = head , *temp2;

          while (temp1!=nullptr)
          {
              temp1 = temp1->next;
              length++;
          }
            
          if (n == length)
          {
              temp2 = head;
              head = head->next;
              delete temp2;
          }
          else
          {
              temp1 = head;
              for (int i = 0; i < length - n - 1; i++ )
              {
                  temp1 = temp1->next;
              }
              temp1->next = temp1->next->next;
          }
          return head;
      }
  };



int main()
{
    vector<int> head = { 1,2 ,3,4 ,5};

    ListNode* head_test = new ListNode(0);
    ListNode* test  , *cur = head_test;
    Solution  a;

    for (int i = 0; i < head.size(); i++)
    {
       
       ListNode* temp = new ListNode(head[i]);
       cur->next = temp;
       cur = cur->next;
      
    }
    cur->next = nullptr;

    cur = head_test;
    cout << "cur list" << endl;
    while (cur->next != nullptr)
    {
        cout << cur->val << ' ';
        cur = cur->next;
    }
    cout << cur->val << endl;


    test = a.removeNthFromEnd(head_test->next,2);

    while (test->next != nullptr)
    {
        cout << test->val << ' ';
        test = test->next;
    }
    cout << test->val << ' ';
	return 0;

}

双指针
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        int sum_node = 0;
        ListNode *real_head = new ListNode(0,head);
        ListNode *tmp = real_head;
        
        while(tmp != nullptr)
        {
            sum_node++;
            tmp = tmp->next;
        }
       
        sum_node = sum_node - n -1;
        tmp = real_head;
        while(sum_node--) tmp = tmp->next;

        if( tmp->next != nullptr && tmp->next->next != nullptr)
        {
            tmp->next = tmp->next->next;
        }else tmp->next = nullptr;

        return real_head->next;
    }
};

面试题 02.07. 链表相交

面试题 02.07. 链表相交 - 力扣(LeetCode)

描述

给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。

图示两个链表在节点 c1 开始相交:
算法学习——LeetCode力扣链表篇2_第4张图片

题目数据 保证 整个链式结构中不存在环。

注意,函数返回结果后,链表必须 保持其原始结构 。

示例 1:

算法学习——LeetCode力扣链表篇2_第5张图片

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at ‘8’
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

示例 2:

算法学习——LeetCode力扣链表篇2_第6张图片

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at ‘2’
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

算法学习——LeetCode力扣链表篇2_第7张图片

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。

提示

  • listA 中节点数目为 m
  • listB 中节点数目为 n
  • 0 <= m, n <= 3 * 104
  • 1 <= Node.val <= 105
  • 0 <= skipA <= m
  • 0 <= skipB <= n
  • 如果 listA 和 listB 没有交点,intersectVal 为 0
  • 如果 listA 和 listB 有交点,intersectVal == listA[skipA + 1] == listB[skipB + 1]

进阶

你能否设计一个时间复杂度 O(n) 、仅用 O(1) 内存的解决方案?

代码解析

暴力循环
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {

        ListNode *dummy_a = new ListNode(0,NULL);
        ListNode *dummy_b = new ListNode(0,NULL);

        ListNode *temp, *tempA , *tempB;

        int flag = 0;
        dummy_a->next = headA;
        dummy_b->next = headB;
        tempA = dummy_a;
        tempB = dummy_b;

        if(headA == headB) return headA;

        while(tempA->next != NULL)
        {
            while(tempB->next != NULL)
            {
                if(tempA == tempB)
                {
                    return tempA; 
                }
                tempB = tempB->next;
            }
           
            tempB = dummy_b;
            tempA = tempA->next;
        }

        while(dummy_b)
        {
            if(tempA == dummy_b)return tempA;
            dummy_b = dummy_b->next;
        }
         while(dummy_a)
        {
            if(tempB == dummy_a)return tempB;
            dummy_a = dummy_a->next;
        }
        return NULL;

    }
};

对其相同部分

先计算两个链表的长度,因为后面完全一致,让长的链表先向后移动到和短链表相同长度。再依次查找是否完全一致的节点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int length_A = 0 , length_B = 0 ,diff=0 ;
        ListNode *cur_A = headA , *cur_B = headB;
        ListNode *dummy_a = new ListNode(0);
        ListNode *dummy_b = new ListNode(0);

        dummy_a->next = headA;
        dummy_b->next = headB;

        while(dummy_a->next != NULL)
        {
            length_A++;
            dummy_a = dummy_a->next;
        }

        while(dummy_b->next != NULL)
        {
            length_B++;
            dummy_b = dummy_b->next;
        }
       
        if(length_A >= length_B)
        {
            for(int i =0;i< length_A-length_B;i++)
            {
                cur_A = cur_A->next;
            }
        }else
        {
            for(int i =0;i< length_B-length_A;i++)
            {
                cur_B = cur_B->next;
            }
        }

        while(cur_A != cur_B)
        {
            cur_A = cur_A->next;
            cur_B = cur_B->next;
        }
        return cur_A;
    }
};

循环法
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* tmpA = headA;
        ListNode* tmpB = headB;

        while(tmpA != tmpB)
        {
            tmpA = (tmpA == nullptr ? headB : tmpA->next);
            tmpB = (tmpB == nullptr ? headA : tmpB->next);
        }
        return tmpA;
    }
};

142. 环形链表 II

142. 环形链表 II - 力扣(LeetCode)

描述

给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

不允许修改 链表。

示例

示例 1:

算法学习——LeetCode力扣链表篇2_第8张图片

输入:head = [3,2,0,-4], pos = 1
输出:返回索引为 1 的链表节点
解释:链表中有一个环,其尾部连接到第二个节点。

示例 2:

算法学习——LeetCode力扣链表篇2_第9张图片

输入:head = [1,2], pos = 0
输出:返回索引为 0 的链表节点
解释:链表中有一个环,其尾部连接到第一个节点。

示例 3:

在这里插入图片描述

输入:head = [1], pos = -1
输出:返回 null
解释:链表中没有环。

提示

链表中节点的数目范围在范围 [0, 104] 内
-105 <= Node.val <= 105
pos 的值为 -1 或者链表中的一个有效索引

进阶

你是否可以使用 O(1) 空间解决此题?

代码解析

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode *left = head;
        ListNode *right = head;
        
        while(right != nullptr && right->next != nullptr)
        {
            right = right->next->next;
            left = left->next;
            if(right == left)
            {
                ListNode *indnx1 = head;
                ListNode *indnx2 = right;
                while(1)
                {
                    if(indnx1 == indnx2) return indnx1;
                    indnx1 = indnx1->next;
                    indnx2 = indnx2->next;
                }
            }   
        }
        return nullptr;
    }
};

你可能感兴趣的:(LeetCode算法学习,算法,学习,leetcode,c++,c语言,c,链表)