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

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Golang全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

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

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

如果你需要这些资料,可以添加V获取:vip1024b (备注go)
img

正文

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 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& res,ListNode* head){
if(head==NULL) return;
method(res,head->next);
res.push_back(head->val);
}
vector reversePrint(ListNode* head) {
vector 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 reversePrint(ListNode* head) {
stack s;
vector 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 reversePrint(ListNode* head) {
vector 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 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)) {
return temp;
}
temp = temp->next;
}
return nullptr;
}
};

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

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

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
if (headA == NULL || headB == NULL) {
return NULL;
}
struct ListNode *pA = headA, *pB = headB;
while (pA != pB) {
pA = pA == NULL ? headB : pA->next;
pB = pB == NULL ? headA : pB->next;
}
return pA;
}


class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == nullptr || headB == nullptr) {
return nullptr;
}
ListNode *pA = headA, *pB = headB;
while (pA != pB) {
pA = pA == nullptr ? headB : pA->next;
pB = pB == nullptr ? headA : pB->next;
}
return pA;
}
};

83. 删除排序链表中的重复元素

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

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

struct ListNode* deleteDuplicates(struct ListNode* head) {
if (!head) {
return head;
}

struct ListNode* cur = head;
while (cur->next) {
if (cur->val == cur->next->val) {
cur->next = cur->next->next;
} else {
cur = cur->next;
}
}

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* deleteDuplicates(ListNode* head) {
if (!head) {
return head;
}

ListNode* cur = head;
while (cur->next) {
if (cur->val == cur->next->val) {
cur->next = cur->next->next;
}
else {
cur = cur->next;
}
}

return head;
}
};

142. 环形链表 II

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2doYWEj5-1631410080363)(https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210905214936206.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);
}

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


class Solution {
public:
ListNode *detectCycle(ListNode *head) {
unordered_set<ListNode *> visited;
while (head != nullptr) {
if (visited.count(head)) {
return head;
}
visited.insert(head);
head = head->next;
}
return nullptr;
}
};

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

struct ListNode* detectCycle(struct ListNode* head) {
struct ListNode *slow = head, *fast = head;
while (fast != NULL) {
slow = slow->next;
if (fast->next == NULL) {
return NULL;
}
fast = fast->next->next;
if (fast == slow) {
struct ListNode* ptr = head;
while (ptr != slow) {
ptr = ptr->next;
slow = slow->next;
}
return ptr;
}
}
return NULL;
}


class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while (fast != nullptr) {
slow = slow->next;
if (fast->next == nullptr) {
return nullptr;
}
fast = fast->next->next;
if (fast == slow) {
ListNode *ptr = head;
while (ptr != slow) {
ptr = ptr->next;
slow = slow->next;
}
return ptr;
}
}
return nullptr;
}
};

203. 移除链表元素

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

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

struct ListNode* removeElements(struct ListNode* head, int val) {
if (head == NULL) {
return head;
}
head->next = removeElements(head->next, val);
return head->val == val ? head->next : head;
}


class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if (head == nullptr) {
return head;
}
head->next = removeElements(head->next, val);
return head->val == val ? head->next : head;
}
};

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

struct ListNode* removeElements(struct ListNode* head, int val) {
struct ListNode* dummyHead = malloc(sizeof(struct ListNode));
dummyHead->next = head;
struct ListNode* temp = dummyHead;
while (temp->next != NULL) {
if (temp->next->val == val) {
temp->next = temp->next->next;
} else {
temp = temp->next;
}
}
return dummyHead->next;
}


class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
struct ListNode* dummyHead = new ListNode(0, head);
struct ListNode* temp = dummyHead;
while (temp->next != NULL) {
if (temp->next->val == val) {
temp->next = temp->next->next;
} else {
temp = temp->next;
}
}
return dummyHead->next;
}
};

876. 链表的中间结点

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

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

class Solution {
public:
ListNode* middleNode(ListNode* head) {
vector<ListNode*> A = {head};
while (A.back()->next != NULL)
A.push_back(A.back()->next);
return A[A.size() / 2];
}
};

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

class Solution {
public:
ListNode* middleNode(ListNode* head) {
int n = 0;
ListNode* cur = head;
while (cur != nullptr) {
++n;
cur = cur->next;
}
int k = 0;
cur = head;
while (k < n / 2) {
++k;
cur = cur->next;
}
return cur;
}
};

image-20210905215910422

class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};

剑指 Offer 18. 删除链表的节点

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

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

class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if(head->val == val) return head->next;
ListNode *pre = head, *cur = head->next;
while(cur != nullptr && cur->val != val) {
pre = cur;
cur = cur->next;
}
if(cur != nullptr) pre->next = cur->next;
return head;
}
};

146. LRU 缓存机制

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

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

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

struct DLinkedNode {
int key, value;
DLinkedNode* prev;
DLinkedNode* next;
DLinkedNode(): key(0), value(0), prev(nullptr), next(nullptr) {}
DLinkedNode(int _key, int _value): key(_key), value(_value), prev(nullptr), next(nullptr) {}
};

class LRUCache {
private:
unordered_map<int, DLinkedNode*> cache;
DLinkedNode* head;
DLinkedNode* tail;
int size;
int capacity;

public:
LRUCache(int _capacity): capacity(_capacity), size(0) {
// 使用伪头部和伪尾部节点
head = new DLinkedNode();
tail = new DLinkedNode();
head->next = tail;
tail->prev = head;
}

int get(int key) {
if (!cache.count(key)) {
return -1;
}
// 如果 key 存在,先通过哈希表定位,再移到头部
DLinkedNode* node = cache[key];
moveToHead(node);
return node->value;
}

void put(int key, int value) {
if (!cache.count(key)) {
// 如果 key 不存在,创建一个新的节点
DLinkedNode* node = new DLinkedNode(key, value);
// 添加进哈希表
cache[key] = node;
// 添加至双向链表的头部
addToHead(node);
++size;
if (size > capacity) {
// 如果超出容量,删除双向链表的尾部节点
DLinkedNode* removed = removeTail();
// 删除哈希表中对应的项
cache.erase(removed->key);
// 防止内存泄漏
delete removed;
–size;
}
}
else {
// 如果 key 存在,先通过哈希表定位,再修改 value,并移到头部
DLinkedNode* node = cache[key];
node->value = value;
moveToHead(node);
}
}

void addToHead(DLinkedNode* node) {
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}

void removeNode(DLinkedNode* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
}

void moveToHead(DLinkedNode* node) {
removeNode(node);
addToHead(node);
}

DLinkedNode* removeTail() {
DLinkedNode* node = tail->prev;
removeNode(node);
return node;
}
};

148. 排序链表

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

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
v = head;
}

int get(int key) {
if (!cache.count(key)) {
return -1;
}
// 如果 key 存在,先通过哈希表定位,再移到头部
DLinkedNode* node = cache[key];
moveToHead(node);
return node->value;
}

void put(int key, int value) {
if (!cache.count(key)) {
// 如果 key 不存在,创建一个新的节点
DLinkedNode* node = new DLinkedNode(key, value);
// 添加进哈希表
cache[key] = node;
// 添加至双向链表的头部
addToHead(node);
++size;
if (size > capacity) {
// 如果超出容量,删除双向链表的尾部节点
DLinkedNode* removed = removeTail();
// 删除哈希表中对应的项
cache.erase(removed->key);
// 防止内存泄漏
delete removed;
–size;
}
}
else {
// 如果 key 存在,先通过哈希表定位,再修改 value,并移到头部
DLinkedNode* node = cache[key];
node->value = value;
moveToHead(node);
}
}

void addToHead(DLinkedNode* node) {
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}

void removeNode(DLinkedNode* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
}

void moveToHead(DLinkedNode* node) {
removeNode(node);
addToHead(node);
}

DLinkedNode* removeTail() {
DLinkedNode* node = tail->prev;
removeNode(node);
return node;
}
};

148. 排序链表

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

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-Hy0slMZc-1713437358843)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的内容,推荐的LeetCode刷题顺序是按照题目刷题,优先选择树、链表、二分查找、DFS、BFS、动态规划等常见型的题目。可以先做2~4道简单题,然后再做中等难度的题目。在选择题目时,可以优先选择题目序号小、点赞多、提交成功率高的题目,这样可以从简单入手,节省时间。同时,LeetCode每道题目都有“模拟面试”功能,可以给自己设定时间限制,如果做不出来可以看答案,然后记住思路后再自己尝试一遍。每种型的题目做完10+道后,可以总结规律。 根据引用\[3\]的内容,题目可以按照不同的分类进行刷题,比如数组与贪心算法、子数组与贪心算法、子序列与贪心算法、数字与贪心、单调栈法、双指针法等。可以根据自己的兴趣和需求选择相应的题目进行刷题。 综上所述,LeetCode刷题顺序可以按照题目型或者题目分类进行选择。 #### 引用[.reference_title] - *1* [LeetCode 刷题顺序,按标签分类,科学刷题!](https://blog.csdn.net/fengyuyeguirenenen/article/details/125099023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [leetcode 刷题指南 & 刷题顺序](https://blog.csdn.net/qijingpei/article/details/125561071)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [leetcode-刷题顺序推荐](https://blog.csdn.net/weixin_38087674/article/details/114107841)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值