链表高频题
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| |
| struct ListNode { |
| int val; |
| ListNode *next; |
| |
| ListNode(int x) : val(x), next(NULL) {} |
| }; |
| |
| class Solution { |
| public: |
| ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { |
| if (headA == nullptr || headB == nullptr) return nullptr; |
| ListNode *a = headA; |
| ListNode *b = headB; |
| int diff = 0; |
| while (a->next != nullptr) { |
| a = a->next; |
| diff++; |
| } |
| while (b->next != nullptr) { |
| b = b->next; |
| diff--; |
| } |
| |
| if (a != b) return nullptr; |
| |
| if (diff > 0) { |
| a = headA; |
| b = headB; |
| } else { |
| a = headB; |
| b = headA; |
| } |
| diff = abs(diff); |
| |
| while (diff-- != 0) |
| a = a->next; |
| |
| while (a != b) { |
| a = a->next; |
| b = b->next; |
| } |
| return a; |
| } |
| }; |
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| 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 *reverseList(ListNode *head, ListNode *tail) { |
| ListNode *pre = tail->next, *cur = head, *next; |
| ListNode *nextNode = tail->next; |
| while (cur != nullptr && cur != nextNode) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| return pre; |
| } |
| |
| ListNode *reverseKGroup(ListNode *head, int k) { |
| |
| ListNode *dummyHead = new ListNode(0, head); |
| ListNode *pre = dummyHead; |
| ListNode *left = dummyHead; |
| ListNode *right; |
| |
| int count; |
| |
| while (pre->next != nullptr) { |
| |
| left = pre->next; |
| right = left; |
| |
| for (count = k - 1; count != 0 && right->next != nullptr; count--) |
| right = right->next; |
| |
| if (count != 0) return dummyHead->next; |
| |
| pre->next = reverseList(left, right); |
| |
| pre = left; |
| } |
| |
| return dummyHead->next; |
| } |
| }; |
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| class Node { |
| public: |
| int val; |
| Node *next; |
| Node *random; |
| |
| Node(int _val) { |
| val = _val; |
| next = nullptr; |
| random = nullptr; |
| } |
| }; |
| |
| class Solution { |
| public: |
| Node *copyRandomList(Node *head) { |
| if (head == nullptr) return nullptr; |
| Node *pre = head; |
| |
| |
| while (pre != nullptr) { |
| |
| Node *node = new Node(pre->val); |
| |
| node->next = pre->next; |
| pre->next = node; |
| pre = pre->next->next; |
| } |
| |
| pre = head; |
| |
| while (pre != nullptr) { |
| if (pre->random != nullptr) |
| pre->next->random = pre->random->next; |
| pre = pre->next->next; |
| } |
| |
| pre = head; |
| Node *res = head->next; |
| Node *cur = head->next; |
| |
| while (cur != nullptr && cur->next != nullptr) { |
| |
| pre->next = pre->next->next; |
| pre = pre->next; |
| |
| cur->next = cur->next->next; |
| cur = cur->next; |
| } |
| |
| pre->next = nullptr; |
| return res; |
| } |
| }; |
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| 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 *findMid(ListNode *head) { |
| ListNode *slow = head; |
| ListNode *fast = head; |
| while (fast != nullptr && fast->next != nullptr) { |
| fast = fast->next->next; |
| slow = slow->next; |
| } |
| return slow; |
| } |
| |
| |
| ListNode *reverseList(ListNode *head) { |
| ListNode *pre = nullptr; |
| ListNode *cur = head; |
| ListNode *next; |
| while (cur != nullptr) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| return pre; |
| } |
| |
| bool isPalindrome(ListNode *head) { |
| ListNode *mid = findMid(head); |
| mid = reverseList(mid); |
| |
| ListNode *p = head; |
| ListNode *q = mid; |
| while (q != nullptr) { |
| if (p->val != q->val) return false; |
| p = p->next; |
| q = q->next; |
| } |
| return true; |
| } |
| }; |
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| 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) { |
| slow = slow->next; |
| fast = fast->next->next; |
| |
| |
| |
| if (slow == fast) { |
| fast = head; |
| |
| while (slow != fast) { |
| slow = slow->next; |
| fast = fast->next; |
| } |
| |
| return slow; |
| } |
| } |
| |
| return nullptr; |
| } |
| }; |
| #include <vector> |
| #include <iostream> |
| #include <algorithm> |
| |
| using namespace std; |
| |
| 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 *merge(ListNode *l1, ListNode *l2) { |
| if (l1 == nullptr || l2 == nullptr) return l1 == nullptr ? l2 : l1; |
| ListNode *dummyHead = new ListNode(); |
| ListNode *pre = dummyHead; |
| while (l1 != nullptr && l2 != nullptr) { |
| if (l1->val < l2->val) { |
| pre->next = l1; |
| l1 = l1->next; |
| } else { |
| pre->next = l2; |
| l2 = l2->next; |
| } |
| pre = pre->next; |
| } |
| if (l1 != nullptr) pre->next = l1; |
| if (l2 != nullptr) pre->next = l2; |
| return dummyHead->next; |
| } |
| |
| |
| ListNode *sortList(ListNode *head) { |
| |
| int len = 0; |
| ListNode *temp = head; |
| while (temp != nullptr) { |
| len++; |
| temp = temp->next; |
| } |
| |
| ListNode *dummyHead = new ListNode(); |
| dummyHead->next = head; |
| |
| |
| for (int gap = 1; gap < len; gap <<= 1) { |
| ListNode *pre = dummyHead; |
| ListNode *cur = dummyHead->next; |
| |
| |
| while (cur != nullptr) { |
| |
| ListNode *l1 = cur; |
| int i = 1; |
| while (i < gap && cur->next != nullptr) { |
| cur = cur->next; |
| i++; |
| } |
| |
| |
| ListNode *l2 = cur->next; |
| |
| cur->next = nullptr; |
| |
| |
| |
| cur = l2; |
| i = 1; |
| while (i < gap && cur != nullptr && cur->next != nullptr) { |
| cur = cur->next; |
| i++; |
| } |
| |
| ListNode *next = nullptr; |
| |
| if (cur != nullptr) { |
| |
| next = cur->next; |
| |
| cur->next = nullptr; |
| } |
| |
| |
| pre->next = merge(l1, l2); |
| |
| while (pre->next != nullptr) |
| pre = pre->next; |
| |
| cur = next; |
| } |
| } |
| |
| return dummyHead->next; |
| } |
| }; |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18438640
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2021-09-28 锁的分类
2021-09-28 synchronized锁的内容
2021-09-28 ReentrantLock
2021-09-28 虚假唤醒