【LeetCode刷题日记】链表类题目常见题型_leetcode链表题型分类(1)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

21. 合并两个有序链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ux7cp8bi-1631410080337)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905170645075.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CnO95sQb-1631410080339)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905170953580.png)]

class Solution {
public:
    ListNode\* mergeTwoLists(ListNode\* l1, ListNode\* l2) {
        if (l1 == nullptr) {
            return l2;
        } else if (l2 == nullptr) {
            return l1;
        } else if (l1->val < l2->val) {
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        } else {
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2M02bxdB-1631410080340)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905171310198.png)]

class Solution {
public:
    ListNode\* mergeTwoLists(ListNode\* l1, ListNode\* l2) {
        ListNode\* preHead = new ListNode(-1);

        ListNode\* prev = preHead;
        while (l1 != nullptr && l2 != nullptr) {
            if (l1->val < l2->val) {
                prev->next = l1;
                l1 = l1->next;
            } else {
                prev->next = l2;
                l2 = l2->next;
            }
            prev = prev->next;
        }

        // 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
        prev->next = l1 == nullptr ? l2 : l1;

        return preHead->next;
    }
};

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sfyEpXNQ-1631410080341)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905171900612.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xqhWsQGB-1631410080341)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905172040030.png)]

int getLength(struct ListNode\* head) {
    int length = 0;
    while (head) {
        ++length;
        head = head->next;
    }
    return length;
}

struct ListNode\* removeNthFromEnd(struct ListNode\* head, int n) {
    struct ListNode\* dummy = malloc(sizeof(struct ListNode));
    dummy->val = 0, dummy->next = head;
    int length = getLength(head);
    struct ListNode\* cur = dummy;
    for (int i = 1; i < length - n + 1; ++i) {
        cur = cur->next;
    }
    cur->next = cur->next->next;
    struct ListNode\* ans = dummy->next;
    free(dummy);
    return ans;
}

-----------------------------------------------------------------
/\*\*
 \* 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:
    int getLength(ListNode\* head) {
        int length = 0;
        while (head) {
            ++length;
            head = head->next;
        }
        return length;
    }

    ListNode\* removeNthFromEnd(ListNode\* head, int n) {
        ListNode\* dummy = new ListNode(0, head);
        int length = getLength(head);
        ListNode\* cur = dummy;
        for (int i = 1; i < length - n + 1; ++i) {
            cur = cur->next;
        }
        cur->next = cur->next->next;
        ListNode\* ans = dummy->next;
        delete dummy;
        return ans;
    }
};


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4ddtjT5a-1631410080342)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905172302647.png)]

struct Stack {
    struct ListNode\* val;
    struct Stack\* next;
};

struct ListNode\* removeNthFromEnd(struct ListNode\* head, int n) {
    struct ListNode\* dummy = malloc(sizeof(struct ListNode));
    dummy->val = 0, dummy->next = head;
    struct Stack\* stk = NULL;
    struct ListNode\* cur = dummy;
    while (cur) {
        struct Stack\* tmp = malloc(sizeof(struct Stack));
        tmp->val = cur, tmp->next = stk;
        stk = tmp;
        cur = cur->next;
    }
    for (int i = 0; i < n; ++i) {
        struct Stack\* tmp = stk->next;
        free(stk);
        stk = tmp;
    }
    struct ListNode\* prev = stk->val;
    prev->next = prev->next->next;
    struct ListNode\* ans = dummy->next;
    free(dummy);
    return ans;
}

---------------------------------------------------------------------
/\*\*
 \* 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\* dummy = new ListNode(0, head);
        stack<ListNode\*> stk;
        ListNode\* cur = dummy;
        while (cur) {
            stk.push(cur);
            cur = cur->next;
        }
        for (int i = 0; i < n; ++i) {
            stk.pop();
        }
        ListNode\* prev = stk.top();
        prev->next = prev->next->next;
        ListNode\* ans = dummy->next;
        delete dummy;
        return ans;
    }
};


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1KHK0Drz-1631410080343)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905172356807.png)]

p3

struct ListNode\* removeNthFromEnd(struct ListNode\* head, int n) {
    struct ListNode\* dummy = malloc(sizeof(struct ListNode));
    dummy->val = 0, dummy->next = head;
    struct ListNode\* first = head;
    struct ListNode\* second = dummy;
    for (int i = 0; i < n; ++i) {
        first = first->next;
    }
    while (first) {
        first = first->next;
        second = second->next;
    }
    second->next = second->next->next;
    struct ListNode\* ans = dummy->next;
    free(dummy);
    return ans;
}

---------------------------------------------------------------
/\*\*
 \* 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\* dummy = new ListNode(0, head);
        ListNode\* first = head;
        ListNode\* second = dummy;
        for (int i = 0; i < n; ++i) {
            first = first->next;
        }
        while (first) {
            first = first->next;
            second = second->next;
        }
        second->next = second->next->next;
        ListNode\* ans = dummy->next;
        delete dummy;
        return ans;
    }
};

2. 两数相加

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MDVCfGZa-1631410080344)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905172714235.png)]

struct ListNode\* addTwoNumbers(struct ListNode\* l1, struct ListNode\* l2) {
    struct ListNode \*head = NULL, \*tail = NULL;
    int carry = 0;
    while (l1 || l2) {
        int n1 = l1 ? l1->val : 0;
        int n2 = l2 ? l2->val : 0;
        int sum = n1 + n2 + carry;
        if (!head) {
            head = tail = malloc(sizeof(struct ListNode));
            tail->val = sum % 10;
            tail->next = NULL;
        } else {
            tail->next = malloc(sizeof(struct ListNode));
            tail->next->val = sum % 10;
            tail = tail->next;
            tail->next = NULL;
        }
        carry = sum / 10;
        if (l1) {
            l1 = l1->next;
        }
        if (l2) {
            l2 = l2->next;
        }
    }
    if (carry > 0) {
        tail->next = malloc(sizeof(struct ListNode));
        tail->next->val = carry;
        tail->next->next = NULL;
    }
    return head;
}

----------------------------------------------------------------------
/\*\*
 \* 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\* addTwoNumbers(ListNode\* l1, ListNode\* l2) {
        ListNode \*head = nullptr, \*tail = nullptr;
        int carry = 0;
        while (l1 || l2) {
            int n1 = l1 ? l1->val: 0;
            int n2 = l2 ? l2->val: 0;
            int sum = n1 + n2 + carry;
            if (!head) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail->next = new ListNode(sum % 10);
                tail = tail->next;
            }
            carry = sum / 10;
            if (l1) {
                l1 = l1->next;
            }
            if (l2) {
                l2 = l2->next;
            }
        }
        if (carry > 0) {
            tail->next = new ListNode(carry);
        }
        return head;
    }
};

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-clARU6jS-1631410080344)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905200722159.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ckZRTkPl-1631410080345)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905200812085.png)]

struct ListNode\* swapPairs(struct ListNode\* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    struct ListNode\* newHead = head->next;
    head->next = swapPairs(newHead->next);
    newHead->next = head;
    return newHead;
}

-----------------------------------------------------------------
class Solution {
public:
    ListNode\* swapPairs(ListNode\* head) {
        if (head == nullptr || head->next == nullptr) {
            return head;
        }
        ListNode\* newHead = head->next;
        head->next = swapPairs(newHead->next);
        newHead->next = head;
        return newHead;
    }
};


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0ahmc7Tm-1631410080345)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905201103933.png)]

struct ListNode\* swapPairs(struct ListNode\* head) {
    struct ListNode dummyHead;
    dummyHead.next = head;
    struct ListNode\* temp = &dummyHead;
    while (temp->next != NULL && temp->next->next != NULL) {
        struct ListNode\* node1 = temp->next;
        struct ListNode\* node2 = temp->next->next;
        temp->next = node2;
        node1->next = node2->next;
        node2->next = node1;
        temp = node1;
    }
    return dummyHead.next;
}

class Solution {
public:
    ListNode\* swapPairs(ListNode\* head) {
        ListNode\* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode\* temp = dummyHead;
        while (temp->next != nullptr && temp->next->next != nullptr) {
            ListNode\* node1 = temp->next;
            ListNode\* node2 = temp->next->next;
            temp->next = node2;
            node1->next = node2->next;
            node2->next = node1;
            temp = node1;
        }
        return dummyHead->next;
    }
};

206. 反转链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mTv5SyrN-1631410080346)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905201236408.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rvlqsjR9-1631410080346)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905201720326.png)]

struct ListNode\* reverseList(struct ListNode\* head) {
    struct ListNode\* prev = NULL;
    struct ListNode\* curr = head;
    while (curr) {
        struct ListNode\* next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

------------------------------------------------------
class Solution {
public:
    ListNode\* reverseList(ListNode\* head) {
        ListNode\* prev = nullptr;
        ListNode\* curr = head;
        while (curr) {
            ListNode\* next = curr->next;
            curr->next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w8vpEfaa-1631410080347)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905201850306.png)]

struct ListNode\* reverseList(struct ListNode\* head) {
    if (head == NULL || head->next == NULL) {
        return head;
    }
    struct ListNode\* newHead = reverseList(head->next);
    head->next->next = head;
    head->next = NULL;
    return newHead;
}

-------------------------------------------------------------------------
class Solution {
public:
    ListNode\* reverseList(ListNode\* head) {
        if (!head || !head->next) {
            return head;
        }
        ListNode\* newHead = reverseList(head->next);
        head->next->next = head;
        head->next = nullptr;
        return newHead;
    }
};

141. 环形链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1jKSUI1k-1631410080347)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905203203031.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A28wIaka-1631410080347)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905203423441.png)]

struct hashTable {
    struct ListNode\* key;
    UT_hash_handle hh;
};

struct hashTable\* hashtable;

struct hashTable\* find(struct ListNode\* ikey) {
    struct hashTable\* tmp;
    HASH\_FIND\_PTR(hashtable, &ikey, tmp);
    return tmp;
}

void insert(struct ListNode\* ikey) {
    struct hashTable\* tmp = malloc(sizeof(struct hashTable));
    tmp->key = ikey;
    HASH\_ADD\_PTR(hashtable, key, tmp);
}

bool hasCycle(struct ListNode\* head) {
    hashtable = NULL;
    while (head != NULL) {
        if (find(head) != NULL) {
            return true;
        }
        insert(head);
        head = head->next;
    }
    return false;
}

---------------------------------------------------------------
/\*\*
 \* Definition for singly-linked list.
 \* struct ListNode {
 \* int val;
 \* ListNode \*next;
 \* ListNode(int x) : val(x), next(NULL) {}
 \* };
 \*/
class Solution {
public:
    bool hasCycle(ListNode \*head) {
        unordered_set<ListNode\*> seen;
        while (head != nullptr) {
            if (seen.count(head)) {
                return true;
            }
            seen.insert(head);
            head = head->next;
        }
        return false;
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RW6Db3Jn-1631410080348)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905203518134.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cwgke1vk-1631410080348)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905203615964.png)]

bool hasCycle(struct ListNode\* head) {
    if (head == NULL || head->next == NULL) {
        return false;
    }
    struct ListNode\* slow = head;
    struct ListNode\* fast = head->next;
    while (slow != fast) {
        if (fast == NULL || fast->next == NULL) {
            return false;
        }
        slow = slow->next;
        fast = fast->next->next;
    }
    return true;
}

--------------------------------------------------------------------------
class Solution {
public:
    bool hasCycle(ListNode\* head) {
        if (head == nullptr || head->next == nullptr) {
            return false;
        }
        ListNode\* slow = head;
        ListNode\* fast = head->next;
        while (slow != fast) {
            if (fast == nullptr || fast->next == nullptr) {
                return false;
            }
            slow = slow->next;
            fast = fast->next->next;
        }
        return true;
    }
};

剑指 Offer 22. 链表中倒数第k个节点

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GO2cHsqJ-1631410080349)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905203757623.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vzqVUaW3-1631410080349)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905203823175.png)]

/\*\*
 \* Definition for singly-linked list.
 \* struct ListNode {
 \* int val;
 \* ListNode \*next;
 \* ListNode(int x) : val(x), next(NULL) {}
 \* };
 \*/
class Solution {
public:
    ListNode\* getKthFromEnd(ListNode\* head, int k) {
        int n = 0;
        ListNode\* node = nullptr;

        for (node = head; node; node = node->next) {
            n++;
        }
        for (node = head; n > k; n--) {
            node = node->next;
        }
      
        return node;
    }
};


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OKcNKbgb-1631410080350)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905203919520.png)]

/\*\*
 \* Definition for singly-linked list.
 \* struct ListNode {
 \* int val;
 \* ListNode \*next;
 \* ListNode(int x) : val(x), next(NULL) {}
 \* };
 \*/
class Solution {
public:
    ListNode\* getKthFromEnd(ListNode\* head, int k) {
        ListNode\* fast = head;
        ListNode\* slow = head;

        while (fast && k > 0) {
            fast = fast->next;
            k--;
        }
        while (fast) {
            fast = fast->next;
            slow = slow->next;
        }

        return slow;
    }
};

25. K 个一组翻转链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hOy2NZ6V-1631410080350)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905204059546.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V7LiIt70-1631410080350)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905204119590.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zk9UL32c-1631410080351)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905204246343.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DIcgj47J-1631410080351)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905204303965.png)]

/\*\*
 \* 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:
    // 翻转一个子链表,并且返回新的头与尾
    pair<ListNode\*, ListNode\*> myReverse(ListNode\* head, ListNode\* tail) {
        ListNode\* prev = tail->next;
        ListNode\* p = head;
        while (prev != tail) {
            ListNode\* nex = p->next;
            p->next = prev;
            prev = p;
            p = nex;
        }
        return {tail, head};
    }

    ListNode\* reverseKGroup(ListNode\* head, int k) {
        ListNode\* hair = new ListNode(0);
        hair->next = head;
        ListNode\* pre = hair;

        while (head) {
            ListNode\* tail = pre;
            // 查看剩余部分长度是否大于等于 k
            for (int i = 0; i < k; ++i) {
                tail = tail->next;
                if (!tail) {
                    return hair->next;
                }
            }
            ListNode\* nex = tail->next;
            // 这里是 C++17 的写法,也可以写成
            // pair<ListNode\*, ListNode\*> result = myReverse(head, tail);
            // head = result.first;
            // tail = result.second;
            tie(head, tail) = myReverse(head, tail);
            // 把子链表重新接回原链表
            pre->next = head;
            tail->next = nex;
            pre = tail;
            head = tail->next;
        }

        return hair->next;
    }
};

23. 合并K个升序链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lLFJHMBH-1631410080352)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905204424400.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PZ4tUpPv-1631410080352)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905204721839.png)]

ListNode\* mergeTwoLists(ListNode \*a, ListNode \*b) {
    if ((!a) || (!b)) return a ? a : b;
    ListNode head, \*tail = &head, \*aPtr = a, \*bPtr = b;
    while (aPtr && bPtr) {
        if (aPtr->val < bPtr->val) {
            tail->next = aPtr; aPtr = aPtr->next;
        } else {
            tail->next = bPtr; bPtr = bPtr->next;
        }
        tail = tail->next;
    }
    tail->next = (aPtr ? aPtr : bPtr);
    return head.next;
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a7u1ODtd-1631410080352)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905204816853.png)]

/\*\*
 \* 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\* mergeTwoLists(ListNode \*a, ListNode \*b) {
        if ((!a) || (!b)) return a ? a : b;
        ListNode head, \*tail = &head, \*aPtr = a, \*bPtr = b;
        while (aPtr && bPtr) {
            if (aPtr->val < bPtr->val) {
                tail->next = aPtr; aPtr = aPtr->next;
            } else {
                tail->next = bPtr; bPtr = bPtr->next;
            }
            tail = tail->next;
        }
        tail->next = (aPtr ? aPtr : bPtr);
        return head.next;
    }

    ListNode\* mergeKLists(vector<ListNode\*>& lists) {
        ListNode \*ans = nullptr;
        for (size_t i = 0; i < lists.size(); ++i) {
            ans = mergeTwoLists(ans, lists[i]);
        }
        return ans;
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fh055srO-1631410080353)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905204858264.png)]

class Solution {
public:
    ListNode\* mergeTwoLists(ListNode \*a, ListNode \*b) {
        if ((!a) || (!b)) return a ? a : b;
        ListNode head, \*tail = &head, \*aPtr = a, \*bPtr = b;
        while (aPtr && bPtr) {
            if (aPtr->val < bPtr->val) {
                tail->next = aPtr; aPtr = aPtr->next;
            } else {
                tail->next = bPtr; bPtr = bPtr->next;
            }
            tail = tail->next;
        }
        tail->next = (aPtr ? aPtr : bPtr);
        return head.next;
    }

    ListNode\* merge(vector <ListNode\*> &lists, int l, int r) {
        if (l == r) return lists[l];
        if (l > r) return nullptr;
        int mid = (l + r) >> 1;
        return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));
    }

    ListNode\* mergeKLists(vector<ListNode\*>& lists) {
        return merge(lists, 0, lists.size() - 1);
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uH7WtZxs-1631410080353)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905205010709.png)]

class Solution {
public:
    struct Status {
        int val;
        ListNode \*ptr;
        bool operator < (const Status &rhs) const {
            return val > rhs.val;
        }
    };

    priority_queue <Status> q;

    ListNode\* mergeKLists(vector<ListNode\*>& lists) {
        for (auto node: lists) {
            if (node) q.push({node->val, node});
        }
        ListNode head, \*tail = &head;
        while (!q.empty()) {
            auto f = q.top(); q.pop();
            tail->next = f.ptr; 
            tail = tail->next;
            if (f.ptr->next) q.push({f.ptr->next->val, f.ptr->next});
        }
        return head.next;
    }
};

剑指 Offer 06. 从尾到头打印链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Hd9K4VbA-1631410080353)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905205117053.png)]

1.回溯法
/\*\*
 \* Definition for singly-linked list.
 \* struct ListNode {
 \* int val;
 \* ListNode \*next;
 \* ListNode(int x) : val(x), next(NULL) {}
 \* };
 \*/
class Solution {
public:
    void method(vector<int>& res,ListNode\* head){
        if(head==NULL) return;
        method(res,head->next);
        res.push\_back(head->val);
    }
    vector<int> reversePrint(ListNode\* head) {
        vector<int> res;
        method(res,head);
        return res;
    }
};

2.用栈
/\*\*
 \* Definition for singly-linked list.
 \* struct ListNode {
 \* int val;
 \* ListNode \*next;
 \* ListNode(int x) : val(x), next(NULL) {}
 \* };
 \*/
class Solution {
public:
    vector<int> reversePrint(ListNode\* head) {
        stack<int> s;
        vector<int> res;
        ListNode\* pre=head;
        while(pre){
            s.push(pre->val);
            pre=pre->next;
        }
        while(!s.empty()){
            res.push\_back(s.top());
            s.pop();
        }
        return res;
    }
};

3.利用vector的insert特性
/\*\*
 \* Definition for singly-linked list.
 \* struct ListNode {
 \* int val;
 \* ListNode \*next;
 \* ListNode(int x) : val(x), next(NULL) {}
 \* };
 \*/
class Solution {
public:
    vector<int> reversePrint(ListNode\* head) {
        vector<int> res;
        ListNode\* pre=head;
        while(pre){
            res.insert(res.begin(),pre->val);
            pre=pre->next;
        }
        return res;
    }
};

92. 反转链表 II

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wn9vVvCa-1631410080354)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905210440394.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ut1kK6BS-1631410080354)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905211255527.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cnMUp0kG-1631410080354)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905211313238.png)]

void reverseLinkedList(struct ListNode \*head) {
    // 也可以使用递归反转一个链表
    struct ListNode \*pre = NULL;
    struct ListNode \*cur = head;

    while (cur != NULL) {
        struct ListNode \*next = cur->next;
        cur->next = pre;
        pre = cur;
        cur = next;
    }
}

struct ListNode \*reverseBetween(struct ListNode \*head, int left, int right) {
    // 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
    struct ListNode \*dummyNode = malloc(sizeof(struct ListNode));
    dummyNode->val = -1;
    dummyNode->next = head;

    struct ListNode \*pre = dummyNode;
    // 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点
    // 建议写在 for 循环里,语义清晰
    for (int i = 0; i < left - 1; i++) {
        pre = pre->next;
    }

    // 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点
    struct ListNode \*rightNode = pre;
    for (int i = 0; i < right - left + 1; i++) {
        rightNode = rightNode->next;
    }

    // 第 3 步:切断出一个子链表(截取链表)
    struct ListNode \*leftNode = pre->next;
    struct ListNode \*curr = rightNode->next;

    // 注意:切断链接
    pre->next = NULL;
    rightNode->next = NULL;

    // 第 4 步:同第 206 题,反转链表的子区间
    reverseLinkedList(leftNode);

    // 第 5 步:接回到原来的链表中
    pre->next = rightNode;
    leftNode->next = curr;
    return dummyNode->next;
}

--------------------------------------------------------

/\*\*
 \* 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 {
private:
    void reverseLinkedList(ListNode \*head) {
        // 也可以使用递归反转一个链表
        ListNode \*pre = nullptr;
        ListNode \*cur = head;

        while (cur != nullptr) {
            ListNode \*next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }
    }

public:
    ListNode \*reverseBetween(ListNode \*head, int left, int right) {
        // 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
        ListNode \*dummyNode = new ListNode(-1);
        dummyNode->next = head;

        ListNode \*pre = dummyNode;
        // 第 1 步:从虚拟头节点走 left - 1 步,来到 left 节点的前一个节点
        // 建议写在 for 循环里,语义清晰
        for (int i = 0; i < left - 1; i++) {
            pre = pre->next;
        }

        // 第 2 步:从 pre 再走 right - left + 1 步,来到 right 节点
        ListNode \*rightNode = pre;
        for (int i = 0; i < right - left + 1; i++) {
            rightNode = rightNode->next;
        }

        // 第 3 步:切断出一个子链表(截取链表)
        ListNode \*leftNode = pre->next;
        ListNode \*curr = rightNode->next;

        // 注意:切断链接
        pre->next = nullptr;
        rightNode->next = nullptr;

        // 第 4 步:同第 206 题,反转链表的子区间
        reverseLinkedList(leftNode);

        // 第 5 步:接回到原来的链表中
        pre->next = rightNode;
        leftNode->next = curr;
        return dummyNode->next;
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WEaxJrfW-1631410080355)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905211444495.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SDaq757O-1631410080355)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905211508445.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3arbBwKD-1631410080356)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905211530179.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UNtAApBS-1631410080356)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905211545955.png)]

struct ListNode \*reverseBetween(struct ListNode \*head, int left, int right) {
    // 因为头节点有可能发生变化,使用虚拟头节点可以避免复杂的分类讨论
    struct ListNode \*dummyNode = malloc(sizeof(struct ListNode));
    dummyNode->val = -1;
    dummyNode->next = head;

    struct ListNode \*pre = dummyNode;
    for (int i = 0; i < left - 1; i++) {
        pre = pre->next;
    }
    struct ListNode \*cur = pre->next;
    struct ListNode \*next;
    for (int i = 0; i < right - left; i++) {
        next = cur->next;
        cur->next = next->next;
        next->next = pre->next;
        pre->next = next;
    }
    return dummyNode->next;
}

class Solution {
public:
    ListNode \*reverseBetween(ListNode \*head, int left, int right) {
        // 设置 dummyNode 是这一类问题的一般做法
        ListNode \*dummyNode = new ListNode(-1);
        dummyNode->next = head;
        ListNode \*pre = dummyNode;
        for (int i = 0; i < left - 1; i++) {
            pre = pre->next;
        }
        ListNode \*cur = pre->next;
        ListNode \*next;
        for (int i = 0; i < right - left; i++) {
            next = cur->next;
            cur->next = next->next;
            next->next = pre->next;
            pre->next = next;
        }
        return dummyNode->next;
    }
};

61. 旋转链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1MDJiQzg-1631410080357)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905211820557.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fZJHOkWS-1631410080357)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905212013766.png)]

struct ListNode\* rotateRight(struct ListNode\* head, int k) {
    if (k == 0 || head == NULL || head->next == NULL) {
        return head;
    }
    int n = 1;
    struct ListNode\* iter = head;
    while (iter->next != NULL) {
        iter = iter->next;
        n++;
    }
    int add = n - k % n;
    if (add == n) {
        return head;
    }
    iter->next = head;
    while (add--) {
        iter = iter->next;
    }
    struct ListNode\* ret = iter->next;
    iter->next = NULL;
    return ret;
}

--------------------------------------
/\*\*
 \* 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\* rotateRight(ListNode\* head, int k) {
        if (k == 0 || head == nullptr || head->next == nullptr) {
            return head;
        }
        int n = 1;
        ListNode\* iter = head;
        while (iter->next != nullptr) {
            iter = iter->next;
            n++;
        }
        int add = n - k % n;
        if (add == n) {
            return head;
        }
        iter->next = head;
        while (add--) {
            iter = iter->next;
        }
        ListNode\* ret = iter->next;
        iter->next = nullptr;
        return ret;
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KqnTy2mK-1631410080358)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905212057168.png)]

234. 回文链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fBOhQ0IO-1631410080358)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905212221447.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B6gUA4tx-1631410080358)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905212515583.png)]

bool isPalindrome(struct ListNode\* head) {
    int vals[50001], vals_num = 0;
    while (head != NULL) {
        vals[vals_num++] = head->val;
        head = head->next;
    }
    for (int i = 0, j = vals_num - 1; i < j; ++i, --j) {
        if (vals[i] != vals[j]) {
            return false;
        }
    }
    return true;
}

------------------------------------------

/\*\*
 \* 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:
    bool isPalindrome(ListNode\* head) {
        vector<int> vals;
        while (head != nullptr) {
            vals.emplace\_back(head->val);
            head = head->next;
        }
        for (int i = 0, j = (int)vals.size() - 1; i < j; ++i, --j) {
            if (vals[i] != vals[j]) {
                return false;
            }
        }
        return true;
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YAoUbP9i-1631410080359)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905212646870.png)]

struct ListNode\* frontPointer;

bool recursivelyCheck(struct ListNode\* currentNode) {
    if (currentNode != NULL) {
        if (!recursivelyCheck(currentNode->next)) {
            return false;
        }
        if (currentNode->val != frontPointer->val) {
            return false;
        }
        frontPointer = frontPointer->next;
    }
    return true;
}

bool isPalindrome(struct ListNode\* head) {
    frontPointer = head;
    return recursivelyCheck(head);
}

---------------------------------------------------------------------
class Solution {
    ListNode\* frontPointer;
public:
    bool recursivelyCheck(ListNode\* currentNode) {
        if (currentNode != nullptr) {
            if (!recursivelyCheck(currentNode->next)) {
                return false;
            }
            if (currentNode->val != frontPointer->val) {
                return false;
            }
            frontPointer = frontPointer->next;
        }
        return true;
    }

    bool isPalindrome(ListNode\* head) {
        frontPointer = head;
        return recursivelyCheck(head);
    }
};

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AKxY3gJ0-1631410080359)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905212745050.png)]

struct ListNode\* reverseList(struct ListNode\* head) {
    struct ListNode\* prev = NULL;
    struct ListNode\* curr = head;
    while (curr != NULL) {
        struct ListNode\* nextTemp = curr->next;
        curr->next = prev;
        prev = curr;
        curr = nextTemp;
    }
    return prev;
}

struct ListNode\* endOfFirstHalf(struct ListNode\* head) {
    struct ListNode\* fast = head;
    struct ListNode\* slow = head;
    while (fast->next != NULL && fast->next->next != NULL) {
        fast = fast->next->next;
        slow = slow->next;
    }
    return slow;
}

bool isPalindrome(struct ListNode\* head) {
    if (head == NULL) {
        return true;
    }

    // 找到前半部分链表的尾节点并反转后半部分链表
    struct ListNode\* firstHalfEnd = endOfFirstHalf(head);
    struct ListNode\* secondHalfStart = reverseList(firstHalfEnd->next);

    // 判断是否回文
    struct ListNode\* p1 = head;
    struct ListNode\* p2 = secondHalfStart;
    bool result = true;
    while (result && p2 != NULL) {
        if (p1->val != p2->val) {
            result = false;
        }
        p1 = p1->next;
        p2 = p2->next;
    }

    // 还原链表并返回结果
    firstHalfEnd->next = reverseList(secondHalfStart);
    return result;
}

------------------------------------------------------------------------
class Solution {
public:
    bool isPalindrome(ListNode\* head) {
        if (head == nullptr) {
            return true;
        }

        // 找到前半部分链表的尾节点并反转后半部分链表
        ListNode\* firstHalfEnd = endOfFirstHalf(head);
        ListNode\* secondHalfStart = reverseList(firstHalfEnd->next);

        // 判断是否回文
        ListNode\* p1 = head;
        ListNode\* p2 = secondHalfStart;
        bool result = true;
        while (result && p2 != nullptr) {
            if (p1->val != p2->val) {
                result = false;
            }
            p1 = p1->next;
            p2 = p2->next;
        }        

        // 还原链表并返回结果
        firstHalfEnd->next = reverseList(secondHalfStart);
        return result;
    }

    ListNode\* reverseList(ListNode\* head) {
        ListNode\* prev = nullptr;
        ListNode\* curr = head;
        while (curr != nullptr) {
            ListNode\* nextTemp = curr->next;
            curr->next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }

    ListNode\* endOfFirstHalf(ListNode\* head) {
        ListNode\* fast = head;
        ListNode\* slow = head;
        while (fast->next != nullptr && fast->next->next != nullptr) {
            fast = fast->next->next;
            slow = slow->next;
        }
        return slow;
    }
};

160. 相交链表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xvbl8czi-1631410080360)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905213134048.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vCpc6BDH-1631410080360)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905213351830.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Li0YuSSE-1631410080360)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905213633355.png)]

struct HashTable {
    struct ListNode \*key;
    UT_hash_handle hh;
};

struct ListNode \*getIntersectionNode(struct ListNode \*headA, struct ListNode \*headB) {
    struct HashTable \*hashTable = NULL;
    struct ListNode \*temp = headA;
    while (temp != NULL) {
        struct HashTable \*tmp;
        HASH\_FIND(hh, hashTable, &temp, sizeof(struct HashTable \*), tmp);
        if (tmp == NULL) {
            tmp = malloc(sizeof(struct HashTable));
            tmp->key = temp;
            HASH\_ADD(hh, hashTable, key, sizeof(struct HashTable \*), tmp);
        }
        temp = temp->next;
    }
    temp = headB;
    while (temp != NULL) {
        struct HashTable \*tmp;
        HASH\_FIND(hh, hashTable, &temp, sizeof(struct HashTable \*), tmp);
        if (tmp != NULL) {
            return temp;
        }
        temp = temp->next;
    }
    return NULL;
}

--------------------------------
/\*\*
 \* 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 != nullptr) {
            visited.insert(temp);
            temp = temp->next;
        }
        temp = headB;
        while (temp != nullptr) {
            if (visited.count(temp)) {


![img](https://img-blog.csdnimg.cn/img_convert/bb13c399c04bf10671ca518401a73a26.png)
![img](https://img-blog.csdnimg.cn/img_convert/a72ab1a84faaa7660095e2ce0daba937.png)
![img](https://img-blog.csdnimg.cn/img_convert/abc81e524665a2b888e64f0ef2c8e95b.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**


        struct HashTable \*tmp;
        HASH\_FIND(hh, hashTable, &temp, sizeof(struct HashTable \*), tmp);
        if (tmp == NULL) {
            tmp = malloc(sizeof(struct HashTable));
            tmp->key = temp;
            HASH\_ADD(hh, hashTable, key, sizeof(struct HashTable \*), tmp);
        }
        temp = temp->next;
    }
    temp = headB;
    while (temp != NULL) {
        struct HashTable \*tmp;
        HASH\_FIND(hh, hashTable, &temp, sizeof(struct HashTable \*), tmp);
        if (tmp != NULL) {
            return temp;
        }
        temp = temp->next;
    }
    return NULL;
}

--------------------------------
/\*\*
 \* 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 != nullptr) {
            visited.insert(temp);
            temp = temp->next;
        }
        temp = headB;
        while (temp != nullptr) {
            if (visited.count(temp)) {


[外链图片转存中...(img-2IFtQPIZ-1715536071623)]
[外链图片转存中...(img-Zbov6QIN-1715536071624)]
[外链图片转存中...(img-C7Rcults-1715536071624)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值