代码随想录学习:链表

目录

一、双指针法

 二、虚拟头结点

三、逻辑推理+数学知识


一、双指针法

力扣题目链接(反转链表)

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* after = nullptr; //慢指针
        ListNode* cur = head; // 快指针

        while(cur){
            ListNode* tmp = cur->next;
            cur->next = after;
            after = cur;
            cur = tmp;
        }
        return after;
    }
};

 二、虚拟头结点

有些题目设置虚拟头结点,可以让操作链表更加的有规律,不用单独考虑头结点

力扣题目链接(两两交换链表中的节点)

ListNode* swapPairs(ListNode* head) {
        ListNode* my_head = new ListNode(0, head);
        ListNode* cur = my_head;
        while(cur->next && cur->next->next){
            ListNode* tmp1 = cur->next;
            ListNode*tmp2 = tmp1->next;
            ListNode*tmp3 = tmp2->next;
            cur->next = tmp2;
            tmp2->next = tmp1;
            tmp1->next = tmp3;
            cur = tmp1;
        }
        ListNode* result = my_head->next;
        delete my_head;
        my_head = nullptr;
        return result;
    }

力扣题目链接(删除链表的倒数第N个节点)

ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* my_head = new ListNode(0,head);
        ListNode* fast = my_head;
        ListNode* slow = my_head;
        int num = n;
        while(fast->next){
            if(num != 0){
                fast = fast->next;
                num--;
                continue;
            }
            fast = fast->next;
            slow = slow->next;
        }
        ListNode* tmp = slow->next;
        slow->next = slow->next->next;
        delete tmp;tmp=nullptr;

        return my_head->next;
    }

三、逻辑推理+数学知识

力扣题目链接(环形链表2)

判断环形链表:快慢指针,快指针一次走两个,慢指针一次走一个,如果相遇就有环;

判断环的起始点的位置,需要数学推理(不看答案根本想不到):

ListNode *detectCycle(ListNode *head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast != NULL && fast->next != NULL) {
            slow = slow->next;
            fast = fast->next->next;
            // 快慢指针相遇,此时从head 和 相遇点,同时查找直至相遇
            if (slow == fast) {
                ListNode* index1 = fast;
                ListNode* index2 = head;
                while (index1 != index2) {
                    index1 = index1->next;
                    index2 = index2->next;
                }
                return index2; // 返回环的入口
            }
        }
        return NULL;
    }

力扣题目链接(链表相交)

先将长的链表的头移动到与短的对齐,然后同时往后移动指针,判断对应指针地址是否相同,如果有相同的则有交点,否则没有。

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode* curA = headA;
        ListNode* curB = headB;
        int lenA = 0, lenB = 0;
        while (curA != NULL) { // 求链表A的长度
            lenA++;
            curA = curA->next;
        }
        while (curB != NULL) { // 求链表B的长度
            lenB++;
            curB = curB->next;
        }
        curA = headA;
        curB = headB;
        // 让curA为最长链表的头,lenA为其长度
        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;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值