代码随想录算法训练营Day4 |Leetcode24两两交换链表中的节点 19删除链表的倒数第N个节点 02.07链表相交 142环形链表II

LeetCode24两两交换链表中的节点

题目链接

递归
ListNode* swapPairs(ListNode* head){
        if (head == nullptr || head->next == nullptr)   return head;
        ListNode* next = head->next;
        head->next = swapPairs(next->next);
        next->next = head;
        return next;
    }
迭代
static ListNode* swapPairs(ListNode* head) {
        ListNode* virtual_head = new ListNode(-1);
        virtual_head->next = head;
        ListNode* cur  = virtual_head;
        while (cur->next != nullptr && cur->next->next != nullptr){
            ListNode* temp1 = cur->next;
            cur->next = cur->next->next;
            temp1->next = cur->next->next;
            cur->next->next = temp1;

            cur = temp1;
        }
        return virtual_head->next;
    }

LeetCode19 删除链表的倒数第N个节点

题目链接

常规解法-先计数后遍历
 ListNode* removeNthFromEnd(ListNode* head, int n) {
      ListNode* p = head;
      int sz = 0;
      while (p != nullptr){
          p = p->next;
          sz++;
      }
      ListNode* virtual_node = new ListNode(-1);
      virtual_node->next = head;
      ListNode* cur = virtual_node;
      n = sz-n;
      while (n>0){
          cur = cur->next;
          n--;
      }
      cur->next = cur->next->next;
      return virtual_node->next;
  }
快慢指针
ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* virtual_node = new ListNode(-1);
        virtual_node->next = head;
        ListNode* fast = virtual_node;
        ListNode* slow = virtual_node;
        while (n-- && fast!= nullptr)   fast = fast->next;
        fast = fast->next;
        while (fast != nullptr){
            slow = slow->next;
            fast = fast->next;
        }
        ListNode* temp = slow->next->next;
        slow->next = temp;
        return virtual_node->next;
    }

LeetCode02.07 链表相交

题目链接

代码
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *p1 = headA;
        ListNode *p2 = headB;
        int sa = 0, sb = 0;
        while (p1 != nullptr){
            sa++;
            p1 = p1->next;
        }
        while (p2 != nullptr){
            sb++;
            p2 = p2->next;
        }
        ListNode* pa = headA;
        ListNode* pb = headB;
        if (sa > sb){
            int gap = sa - sb;
            while (gap--)   pa = pa->next;
        } else{
            int gap = sb - sa;
            while (gap--)   pb = pb->next;
        }
        while (pa != nullptr){
            if (pa == pb) return pa;
            pa = pa->next;
            pb = pb->next;
        }
        return nullptr;
    }

LeetCode142 环形链表II

题目链接

哈希
 ListNode *detectCycle(ListNode *head) {
        unordered_set<ListNode*> hash;
        while (head){
            if (hash.count(head))   return head;
            hash.insert(head);
            head = head->next;
        }
        return nullptr;
    }
快慢指针

思路:

在这里插入图片描述
代码:

ListNode *detectCycle(ListNode *head) {
        ListNode* slow = head;
        ListNode* fast = head;

        while (fast != nullptr && fast->next != nullptr){
            slow = slow->next;
            fast = fast->next->next;
            // 慢指针与快指针相遇,说明存在环
            if (slow == fast){
                // x = (n-1)(y+z)+z
                ListNode* index1 = head;
                ListNode* index2 = fast;
                while (index1 != index2){
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2;
            }
        }
        return nullptr;
    }
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值