代码随想录算法训练营第4天| 24. 两两交换链表中的节点、19.删除链表的倒数第N个节点 、面试题 02.07. 链表相交、142.环形链表II

代码随想录算法训练营第4天| 24. 两两交换链表中的节点19.删除链表的倒数第N个节点 、面试题 02.07. 链表相交、142.环形链表II

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

使用虚拟头节点,借助临时节点进行转换

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* cur = dummyHead;
		while (cur->next != nullptr && cur->next->next != nullptr) {
            //记录两个临时节点
            ListNode* tmp1 = cur->next;
           	ListNode* tmp2 = cur->next->next->next;;
            
            cur->next = cur->next->next;
            cur->next->next = tmp1;
            cur->next->next->next = tmp2;
            
            cur = cur->next->next; //cur后移两位,准备接下来的位移
        }
        return dummyHead->next;
    }
};

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

使用快慢双指针。链表的长度为size,fast走了n步,还剩下了size-n步;然后fast和slow一起开始往后走,一直走到fast到底,fast和slow都走了size-n + 1步,为了方便,在 fast走了之后让其再往后一步,fast = fast->next; 以此,fast 和 slow 都走了 size - n 步,slow 后面的那一个节点就是倒数第 n 个节点。

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* dummyNode = new ListNode(0);
        dummyNode->next = head;
        ListNode* fast = dummyNode, * slow = dummyNode;
        while (n && fast != nullptr) {
            fast = fast->next;
            n--;
        }
        // to reach the front node of deleted node
        fast = fast->next;
        while (fast != nullptr) {
            fast = fast->next;
            slow = slow->next;
        }
        ListNode* tmp = slow->next;
        slow->next = slow->next->next;
        delete tmp;
        return dummyNode->next;
    }
};

面试题 02.07. 链表相交

如果链表在某一个节点相交之后,那么在之后其节点都是一样的。

本题的思路可以是在分别求出两个链表的长度,然后尾部对齐,求得长度的差值,让较长的链表从头结点开始往后移动该长度的步数,接着依次比较是否有相同的节点。

class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *curA = headA, * curB = headB;
        int lenA = 0, lenB = 0;
        // get the length of two linkedlist
        while (curA != nullptr) {
            curA = curA->next;
            lenA++;
        }
        while (curB != nullptr) {
            curB = curB->next;
            lenB++;
        }
        // to renew the head node
        curA = headA;
        curB = headB;
        // to confirm lenA is the bigger one
        if (lenA < lenB) {
            swap(curA, curB);
            swap(lenA, lenB);
        }
        int gap =  lenA - lenB;
        // to comfirm the last linkedlists are the same length
        while (gap--) {
            curA = curA->next;
        }
        while (curA != nullptr) {
            if (curA == curB) {
                return curA;
            }
            curA = curA->next;
            curB = curB->next;
        }
        // the last choice, nullptr
        return nullptr;
    }
};

142.环形链表II

典型的快慢指针。fast 和 slow 相遇在圈内之后,然后用两个指针从头结点和相遇的节点开始,一起往下走,再次相遇的地方就是入口节点。

/*
to defice for single-linked list.
struct ListNode {
	int val;
	ListNode* next;
	ListNode(int x): val(x), next(nullptr){}
};
*/
class Solution {
public:
    ListNode* detectCycle(ListNode* head) {
        ListNode *slow = head, *fast = head;
        while (fast != nullptr && fast->next != nullptr) {
            fast = fast->next->next;
            slow = slow->next;
            //first meet
            if (slow == fast) {
                ListNode *index1 = fast, *index2 = head;
                while (index1 != index2) {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index1;
            }
        }
        return nullptr;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值