代码随想录链表章节二刷day08

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

. - 力扣(LeetCode)

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

1.虚拟头节点顺序模拟:过程为头指向2,2指向1,1指向3,然后下一轮模拟 将头换到1,继续下一轮调换

/**
 * 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) {
        ListNode* dumyHead = new ListNode(0);
        dumyHead->next = head;
        ListNode* cur = dumyHead;
        // tou -> 1 -> 2 -> 3 -> 4
        while(cur->next != nullptr && cur->next->next != nullptr) {
            ListNode* temp1 = cur->next;        // 存1
            ListNode* temp3 = cur->next->next->next;   // 存3
            // 开始改变指向
            // 步骤一:tou -> 2
            cur->next = cur->next->next;
            // 步骤二:tou -> 2 -> 1
            cur->next->next = temp1;
            // 步骤三:tou -> 2 -> 1 -> 3
            cur->next->next->next = temp3;

            // tou 1 2 变换到 tou 3 4 开始下一轮
            cur = cur->next->next;
        }
        return dumyHead->next;
    }
};

2.递归法:递归用来实现两两交换元素和指向下一个递归的节点

/**
 * 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;
        }
        // 递归单元内两个节点做交换

        // 定义新的头节点为head->next(该递归单元的第二个节点)
        ListNode* newHead = head->next;     // 新链表的头节点   2
        // 记录第三个节点同时也是下一次递归的head
        ListNode* temp= newHead->next;
        // 使单元内的第二个节点指向第一个节点  交换  1和2
        newHead->next = head;           // head 原始链表头节点  1
        // 交换完成后的head应该指向下一次递归的位置
        head->next = swapPairs(temp);  // 
        // 返回新的头节点
        return newHead;
    }
};

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

. - 力扣(LeetCode)

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

1.虚拟头节点+双指针:快慢指针同时指向链表的虚拟头节点,让快指针向后移动n位,那么此时,当快指针指向链表结尾的时候,慢指针指向的就是倒数第N个节点

细节点在于:快指针移动N位后再向后移动一位,目的是为了让慢指针指向删除节点的前一位

/**
 * 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) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* slow = dummyHead;
        ListNode* fast = dummyHead;
        while(n-- && fast != nullptr) {
            fast = fast->next;
        }
        // fast 在提前走一步,需要让slow指向删除节点的上一个节点
        fast = fast->next;
        while(fast != nullptr) {
            fast = fast->next;
            slow = slow->next;
        }
        // 找到倒数第N个节点
        slow->next = slow->next->next;
        return dummyHead->next;
    }
};

面试题02.07.链表相交

. - 力扣(LeetCode)

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

题目理解:首先相交不是值相等!!!    如果相交,那么快慢指针的差值就是链表的长度差

1.暴力解法:首先找到每个链表的长度,然后借鉴19双指针解法,让快指针先走长度差,走完长度差 就找到了 相交节点(算出长度后,指针已经指向链表末尾了,需要重新初始化)

/**
 * 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* curA = headA;
        ListNode* curB = headB;
        int lenA = 0, lenB = 0;
        while(curA != NULL) {
            lenA++;
            curA = curA->next;
        }
        while(curB != NULL) {
            lenB++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        // 让curA为最长链表的头
        if(lenB > lenA) {
            swap(lenA, lenB);
            swap(curA, curB);
        }
        // 求长度差
        int gap = lenA - lenB;
        // 让curA和curB在同一起点上(末尾位置对齐)
        while(gap--) {
            curA = curA->next;
        }
        // 遍历curA和curB,遇到相同直接返回
        while(curA != NULL) {
            if(curA == curB) {
                return curA;
            }
            curA = curA->next;
            curB = curB->next;
        }
        return NULL;
    }
};

2.双指针(如果有相交的结点D的话,每条链的头结点先走完自己的链表长度,然后回头走另外的一条链表,那么两结点一定为相交于D点,因为这时每个头结点走的距离是一样的,都是 AD + BD + DC,而他们每次又都是前进1,所以距离相同,速度又相同,固然一定会在相同的时间走到相同的结点上,即D点)

/**
 * 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* curA = headA;
        ListNode* curB = headB;
        while(curA != curB) {
            if(curA == NULL) {
                curA = headB;
            } else {
                curA = curA->next;
            }

            if(curB == NULL) {
                curB = headA;
            } else {
                curB = curB->next;
            }
        }
        return curA;
    }
};

3.哈希集合:用哈希集合存储链表节点,如果当前节点在哈希集合中,则后面的节点都在该集合中,返回该节点

/**
 * 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) {
        unordered_set<ListNode*> visited;
        ListNode* temp = headA;
        while(temp != NULL) {
            visited.insert(temp);
            temp = temp->next;
        }
        temp = headB;
        while(temp != NULL) {
            if(visited.count(temp)) {
                return temp;
            }
            temp = temp->next;
        }
        return NULL;
    }
};

142.环形链表II

. - 力扣(LeetCode)

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

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

不允许修改 链表。

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) {
        unordered_set<ListNode*> visited;
        while(head != NULL) {
            if(visited.count(head)) {
                return head;
            }
            visited.insert(head);
            head = head->next;
        }
        return NULL;
    }
};

2.快慢指针法:从头结点出发,fast指针每次移动两个节点,slow指针每次移动一个节点,如果fast和slow指针在途中相遇,说明这个链表有环        经过推到发现:从头节点出发一个指针,从相遇节点也出发一个指针,这两个指针每次直走一个节点,那么当这两个指针相遇的时候就是环形入口的节点

/**
 * 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* fast = head;
        ListNode* slow = head;
        while(fast != NULL && fast->next != NULL) {
            // 双指针
            slow = slow->next;
            fast = fast->next->next;
            // 快慢指针相遇
            if(slow == fast) {
                ListNode* index1 = fast;
                ListNode* index2 = head;
                while(index1 != index2) {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2;
            }
        }
        return NULL;
    }
};

感谢  代码随想录的讲解!

兄弟们加油!

秋招加油!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值