代码随想录day4 lc24, 19, 02.07, 142

  1. Swap Nodes in Pairs
    https://leetcode.cn/problems/swap-nodes-in-pairs/
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummy = new ListNode(0, head);
        ListNode* copy =dummy;
        while (dummy && dummy->next && dummy->next->next) {
            ListNode* next = dummy->next;
            ListNode* nextnext = dummy->next->next->next;
            dummy->next = dummy->next->next;
            dummy->next->next = next;
            dummy->next->next->next = nextnext;
            dummy = dummy->next->next;
        }
        return copy->next;
    }
};
  1. Remove Nth Node From End of List
    https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummy = new ListNode(0, head);
        ListNode* fast = dummy;
        ListNode* slow = dummy;
        while (n >= 0 && fast) {
            fast = fast->next;
            n--;
        }
        while (fast) {
            fast = fast->next;
            slow = slow->next;
        }
        slow->next = slow->next->next;
        return dummy->next;
    }
};

面试题 02.07. Intersection of Two Linked Lists LCCI
https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/
两个链表尾端对齐,然后让curA移动到和curB 末尾对齐的位置,两个链表同时向后移动,找到相同的两个节点。

class Solution {
public:
    int getLen(ListNode* head) {
        int len = 0;
        while (head) {
            head = head->next;
            len++;
        }
        return len;
    }
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lenA = getLen(headA);
        int lenB = getLen(headB);
        if (lenA < lenB) {
            swap(headA, headB);
            swap(lenA, lenB);
        }
        int gap = lenA - lenB;
        while (gap && headA) {
            headA = headA->next;
            gap--;
        }
        while (headA && headB) {
            if (headA == headB) {
                return headA;
            }
            headA = headA->next;
            headB = headB->next;
        }
        return NULL;
    }
};
  1. Linked List Cycle II
    https://leetcode.cn/problems/linked-list-cycle-ii/
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode* slow = head;
        ListNode* fast = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast) {
                slow = head;
                while (slow != fast) {
                    slow = slow->next;
                    fast = fast->next;
                }
                return slow;
            }
        }
        return NULL;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值