-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
219 lines (199 loc) · 5.73 KB
/
linkedlist.cpp
File metadata and controls
219 lines (199 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Linked List Standard Operations
Mathematical Foundation: Dynamic memory allocation, pointer manipulation
Operations: Insert/Delete O(1), Search O(n)
Applications: LRU Cache, Reverse operations, Cycle detection
*/
#include <bits/stdc++.h>
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) {}
};
// Reverse Linked List
// LeetCode: 206. Reverse Linked List
// https://leetcode.com/problems/reverse-linked-list/
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
while (head) {
ListNode* next = head->next;
head->next = prev;
prev = head;
head = next;
}
return prev;
}
// Merge Two Sorted Lists
// LeetCode: 21. Merge Two Sorted Lists
// https://leetcode.com/problems/merge-two-sorted-lists/
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode dummy;
ListNode* tail = &dummy;
while (l1 && l2) {
if (l1->val <= l2->val) { tail->next = l1; l1 = l1->next; }
else { tail->next = l2; l2 = l2->next; }
tail = tail->next;
}
tail->next = l1 ? l1 : l2;
return dummy.next;
}
// Remove Nth Node From End
// LeetCode: 19. Remove Nth Node From End of List
// https://leetcode.com/problems/remove-nth-node-from-end-of-list/
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode dummy(0, head);
ListNode* fast = &dummy, *slow = &dummy;
for (int i = 0; i <= n; i++) fast = fast->next;
while (fast) { fast = fast->next; slow = slow->next; }
slow->next = slow->next->next;
return dummy.next;
}
// Linked List Cycle
// LeetCode: 141. Linked List Cycle
// https://leetcode.com/problems/linked-list-cycle/
bool hasCycle(ListNode* head) {
ListNode* slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) return true;
}
return false;
}
// Linked List Cycle II
// LeetCode: 142. Linked List Cycle II
// https://leetcode.com/problems/linked-list-cycle-ii/
ListNode* detectCycle(ListNode* head) {
ListNode* slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) break;
}
if (!fast || !fast->next) return nullptr;
slow = head;
while (slow != fast) { slow = slow->next; fast = fast->next; }
return slow;
}
// Middle of Linked List
// LeetCode: 876. Middle of the Linked List
// https://leetcode.com/problems/middle-of-the-linked-list/
ListNode* middleNode(ListNode* head) {
ListNode* slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
// Palindrome Linked List
// LeetCode: 234. Palindrome Linked List
// https://leetcode.com/problems/palindrome-linked-list/
bool isPalindrome(ListNode* head) {
ListNode* slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
slow = reverseList(slow);
while (slow) {
if (head->val != slow->val) return false;
head = head->next;
slow = slow->next;
}
return true;
}
// Intersection of Two Linked Lists
// LeetCode: 160. Intersection of Two Linked Lists
// https://leetcode.com/problems/intersection-of-two-linked-lists/
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
ListNode* a = headA, *b = headB;
while (a != b) {
a = a ? a->next : headB;
b = b ? b->next : headA;
}
return a;
}
// Add Two Numbers
// LeetCode: 2. Add Two Numbers
// https://leetcode.com/problems/add-two-numbers/
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode dummy;
ListNode* cur = &dummy;
int carry = 0;
while (l1 || l2 || carry) {
int sum = carry + (l1 ? l1->val : 0) + (l2 ? l2->val : 0);
carry = sum / 10;
cur->next = new ListNode(sum % 10);
cur = cur->next;
if (l1) l1 = l1->next;
if (l2) l2 = l2->next;
}
return dummy.next;
}
// LRU Cache
// LeetCode: 146. LRU Cache
// https://leetcode.com/problems/lru-cache/
class LRUCache {
struct Node {
int key, val;
Node* prev, *next;
Node(int k = 0, int v = 0) : key(k), val(v), prev(nullptr), next(nullptr) {}
};
unordered_map<int, Node*> mp;
Node* head, *tail;
int cap;
void addNode(Node* node) {
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}
void removeNode(Node* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
}
void moveToHead(Node* node) {
removeNode(node);
addNode(node);
}
Node* popTail() {
Node* last = tail->prev;
removeNode(last);
return last;
}
public:
LRUCache(int capacity) : cap(capacity) {
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
}
int get(int key) {
if (mp.count(key)) {
Node* node = mp[key];
moveToHead(node);
return node->val;
}
return -1;
}
void put(int key, int value) {
if (mp.count(key)) {
Node* node = mp[key];
node->val = value;
moveToHead(node);
} else {
Node* node = new Node(key, value);
if (mp.size() >= cap) {
Node* tail_node = popTail();
mp.erase(tail_node->key);
delete tail_node;
}
addNode(node);
mp[key] = node;
}
}
};