Skip to content

Commit 84e20f5

Browse files
committed
Added section Linked List
1 parent d1aef04 commit 84e20f5

39 files changed

Lines changed: 926 additions & 8 deletions

‎cpp/SUMMARY.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,21 @@
2727
* [Candy](linear-list/array/candy.md)
2828
* [Single Number](linear-list/array/single-number.md)
2929
* [Single Number II](linear-list/array/single-number-ii.md)
30-
* [链表](linear-list/linked-list.md)
30+
* [单链表](linear-list/linked-list/README.md)
31+
* [Add Two Numbers](linear-list/linked-list/add-two-numbers.md)
32+
* [Reverse Linked List II](linear-list/linked-list/reverse-linked-list-ii.md)
33+
* [Partition List](linear-list/linked-list/partition-list.md)
34+
* [Remove Duplicates from Sorted List](linear-list/linked-list/remove-duplicates-from-sorted-list.md)
35+
* [Remove Duplicates from Sorted List II](linear-list/linked-list/remove-duplicates-from-sorted-list-ii.md)
36+
* [Rotate List](linear-list/linked-list/rotate-list.md)
37+
* [Remove Nth Node From End of List](linear-list/linked-list/remove-nth-node-from-end-of-list.md)
38+
* [Swap Nodes in Pairs](linear-list/linked-list/swap-nodes-in-pairs.md)
39+
* [Reverse Nodes in k-Group](linear-list/linked-list/reverse-nodes-in-k-group.md)
40+
* [Copy List with Random Pointer](linear-list/linked-list/copy-list-with-random-pointer.md)
41+
* [Linked List Cycle](linear-list/linked-list/linked-list-cycle.md)
42+
* [Linked List Cycle II](linear-list/linked-list/linked-list-cycle-ii.md)
43+
* [Reorder List](linear-list/linked-list/reorder-list.md)
44+
* [LRU Cache](linear-list/linked-list/lru-cache.md)
3145
* [字符串](string/README.md)
3246
* [栈和队列](stack-and-queue/README.md)
3347
* [栈](stack-and-queue/stack.md)

‎cpp/code/add-two-numbers.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Add Two Numbers
2+
// 跟Add Binary 很类似
3+
// 时间复杂度O(m+n),空间复杂度O(1)
4+
class Solution {
5+
public:
6+
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
7+
ListNode dummy(-1); // 头节点
8+
int carry = 0;
9+
ListNode *prev = &dummy;
10+
for (ListNode *pa = l1, *pb = l2;
11+
pa != nullptr || pb != nullptr;
12+
pa = pa == nullptr ? nullptr : pa->next,
13+
pb = pb == nullptr ? nullptr : pb->next,
14+
prev = prev->next) {
15+
const int ai = pa == nullptr ? 0 : pa->val;
16+
const int bi = pb == nullptr ? 0 : pb->val;
17+
const int value = (ai + bi + carry) % 10;
18+
carry = (ai + bi + carry) / 10;
19+
prev->next = new ListNode(value); // 尾插法
20+
}
21+
if (carry > 0)
22+
prev->next = new ListNode(carry);
23+
return dummy.next;
24+
}
25+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copy List with Random Pointer
2+
// 两遍扫描,时间复杂度O(n),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
RandomListNode *copyRandomList(RandomListNode *head) {
6+
for (RandomListNode* cur = head; cur != nullptr; ) {
7+
RandomListNode* node = new RandomListNode(cur->label);
8+
node->next = cur->next;
9+
cur->next = node;
10+
cur = node->next;
11+
}
12+
13+
for (RandomListNode* cur = head; cur != nullptr; ) {
14+
if (cur->random != NULL)
15+
cur->next->random = cur->random->next;
16+
cur = cur->next->next;
17+
}
18+
19+
// 分拆两个单链表
20+
RandomListNode dummy(-1);
21+
for (RandomListNode* cur = head, *new_cur = &dummy;
22+
cur != nullptr; ) {
23+
new_cur->next = cur->next;
24+
new_cur = new_cur->next;
25+
cur->next = cur->next->next;
26+
cur = cur->next;
27+
}
28+
return dummy.next;
29+
}
30+
};

‎cpp/code/linked-list-cycle-ii.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Linked List Cycle II
2+
// 时间复杂度O(n),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
ListNode *detectCycle(ListNode *head) {
6+
ListNode *slow = head, *fast = head;
7+
while (fast && fast->next) {
8+
slow = slow->next;
9+
fast = fast->next->next;
10+
if (slow == fast) {
11+
ListNode *slow2 = head;
12+
13+
while (slow2 != slow) {
14+
slow2 = slow2->next;
15+
slow = slow->next;
16+
}
17+
return slow2;
18+
}
19+
}
20+
return nullptr;
21+
}
22+
};

‎cpp/code/linked-list-cycle.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Linked List Cycle
2+
// 时间复杂度O(n),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
bool hasCycle(ListNode *head) {
6+
// 设置两个指针,一个快一个慢
7+
ListNode *slow = head, *fast = head;
8+
while (fast && fast->next) {
9+
slow = slow->next;
10+
fast = fast->next->next;
11+
if (slow == fast) return true;
12+
}
13+
return false;
14+
}
15+
};

‎cpp/code/lru-cache.cpp‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// LRU Cache
2+
// 时间复杂度O(logn),空间复杂度O(n)
3+
class LRUCache{
4+
private:
5+
struct CacheNode {
6+
int key;
7+
int value;
8+
CacheNode(int k, int v) :key(k), value(v){}
9+
};
10+
public:
11+
LRUCache(int capacity) {
12+
this->capacity = capacity;
13+
}
14+
15+
int get(int key) {
16+
if (cacheMap.find(key) == cacheMap.end()) return -1;
17+
18+
// 把当前访问的节点移到链表头部,并且更新map中该节点的地址
19+
cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
20+
cacheMap[key] = cacheList.begin();
21+
return cacheMap[key]->value;
22+
}
23+
24+
void set(int key, int value) {
25+
if (cacheMap.find(key) == cacheMap.end()) {
26+
if (cacheList.size() == capacity) { //删除链表尾部节点(最少访问的节点)
27+
cacheMap.erase(cacheList.back().key);
28+
cacheList.pop_back();
29+
}
30+
// 插入新节点到链表头部, 并且在map中增加该节点
31+
cacheList.push_front(CacheNode(key, value));
32+
cacheMap[key] = cacheList.begin();
33+
} else {
34+
//更新节点的值,把当前访问的节点移到链表头部,并且更新map中该节点的地址
35+
cacheMap[key]->value = value;
36+
cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
37+
cacheMap[key] = cacheList.begin();
38+
}
39+
}
40+
private:
41+
list<CacheNode> cacheList;
42+
unordered_map<int, list<CacheNode>::iterator> cacheMap;
43+
int capacity;
44+
};

‎cpp/code/partition-list.cpp‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Partition List
2+
// 时间复杂度O(n),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
ListNode* partition(ListNode* head, int x) {
6+
ListNode left_dummy(-1); // 头结点
7+
ListNode right_dummy(-1); // 头结点
8+
9+
auto left_cur = &left_dummy;
10+
auto right_cur = &right_dummy;
11+
12+
for (ListNode *cur = head; cur; cur = cur->next) {
13+
if (cur->val < x) {
14+
left_cur->next = cur;
15+
left_cur = cur;
16+
} else {
17+
right_cur->next = cur;
18+
right_cur = cur;
19+
}
20+
}
21+
22+
left_cur->next = right_dummy.next;
23+
right_cur->next = nullptr;
24+
25+
return left_dummy.next;
26+
}
27+
};

‎cpp/code/permutation-sequence-2.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// LeetCode, Permutation Sequence
1+
// Permutation Sequence
22
// 康托编码
33
// 时间复杂度O(n),空间复杂度O(1)
44
class Solution {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Remove Duplicates from Sorted List
2+
// 递归版,时间复杂度O(n),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
ListNode *deleteDuplicates(ListNode *head) {
6+
if (!head) return head;
7+
ListNode dummy(head->val + 1); // 值只要跟head不同即可
8+
dummy.next = head;
9+
10+
recur(&dummy, head);
11+
return dummy.next;
12+
}
13+
private:
14+
static void recur(ListNode *prev, ListNode *cur) {
15+
if (cur == nullptr) return;
16+
17+
if (prev->val == cur->val) { // 删除head
18+
prev->next = cur->next;
19+
delete cur;
20+
recur(prev, prev->next);
21+
} else {
22+
recur(prev->next, cur->next);
23+
}
24+
}
25+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Remove Duplicates from Sorted List
2+
// 迭代版,时间复杂度O(n),空间复杂度O(1)
3+
class Solution {
4+
public:
5+
ListNode *deleteDuplicates(ListNode *head) {
6+
if (head == nullptr) return nullptr;
7+
8+
for (ListNode *prev = head, *cur = head->next; cur; cur = prev->next) {
9+
if (prev->val == cur->val) {
10+
prev->next = cur->next;
11+
delete cur;
12+
} else {
13+
prev = cur;
14+
}
15+
}
16+
return head;
17+
}
18+
};

0 commit comments

Comments
 (0)