Linked List¶
Table of Contents¶
- 206. Reverse Linked List (Easy)
- 21. Merge Two Sorted Lists (Easy)
- 143. Reorder List (Medium)
- 19. Remove Nth Node From End of List (Medium)
- 141. Linked List Cycle (Easy)
- 23. Merge k Sorted Lists (Hard)
206. Reverse Linked List¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: linked list, recursion
- Reverse a singly linked list.
graph LR
A((1)) --> B((2))
B --> C((3))
C --> D((4))
D --> E((5))
graph RL
E((5)) --> D((4))
D --> C((3))
C --> B((2))
B --> A((1))
206. Reverse Linked List - Python Solution
from typing import Optional
from template import ListNode
# Iterative
def reverseListIterative(head: Optional[ListNode]) -> Optional[ListNode]:
cur = head
prev = None
while cur:
temp = cur.next
cur.next = prev
prev = cur
cur = temp
return prev
# Recursive
def reverseListRecursive(head: Optional[ListNode]) -> Optional[ListNode]:
def reverse(cur, prev):
if not cur:
return prev
temp = cur.next
cur.next = prev
return reverse(temp, cur)
return reverse(head, None)
nums = [1, 2, 3, 4, 5]
head1 = ListNode.create(nums)
print(head1)
# 1 -> 2 -> 3 -> 4 -> 5
print(reverseListIterative(head1))
# 5 -> 4 -> 3 -> 2 -> 1
head2 = ListNode.create(nums)
print(reverseListRecursive(head2))
# 5 -> 4 -> 3 -> 2 -> 1
21. Merge Two Sorted Lists¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: linked list, recursion
- Merge the two lists into one sorted list.
21. Merge Two Sorted Lists - Python Solution
from typing import Optional
from template import ListNode
# Linked List
def mergeTwoLists(
list1: Optional[ListNode], list2: Optional[ListNode]
) -> Optional[ListNode]:
dummy = ListNode()
cur = dummy
while list1 and list2:
if list1.val < list2.val:
cur.next = list1
list1 = list1.next
else:
cur.next = list2
list2 = list2.next
cur = cur.next
if list1:
cur.next = list1
elif list2:
cur.next = list2
return dummy.next
list1 = ListNode.create([1, 2, 4])
list2 = ListNode.create([1, 3, 4])
print(mergeTwoLists(list1, list2))
# 1 -> 1 -> 2 -> 3 -> 4 -> 4
21. Merge Two Sorted Lists - C++ Solution
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* list1, ListNode* list2) {
ListNode dummy;
ListNode* cur = &dummy;
while (list1 && list2) {
if (list1->val < list2->val) {
cur->next = list1;
list1 = list1->next;
} else {
cur->next = list2;
list2 = list2->next;
}
cur = cur->next;
}
cur->next = list1 ? list1 : list2;
return dummy.next;
}
};
143. Reorder List¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: linked list, two pointers, stack, recursion
143. Reorder List - Python Solution
from typing import Optional
from template import ListNode
# Linked List
def reorderList(head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if not head or not head.next:
return
# Middle of the linked list
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# Reverse the second half
pre, cur = None, slow
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
# Merge two linked lists
first, second = head, pre
while second.next:
temp1, temp2 = first.next, second.next
first.next = second
second.next = temp1
first, second = temp1, temp2
head = ListNode.create([1, 2, 3, 4, 5, 6])
print(head) # 1 -> 2 -> 3 -> 4 -> 5 -> 6
reorderList(head)
print(head) # 1 -> 6 -> 2 -> 5 -> 3 -> 4
19. Remove Nth Node From End of List¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: linked list, two pointers
- Given the
head
of a linked list, remove then-th
node from the end of the list and return its head.
19. Remove Nth Node From End of List - Python Solution
from typing import Optional
from template import ListNode
# Linked List
def removeNthFromEnd(head: Optional[ListNode], n: int) -> Optional[ListNode]:
dummy = ListNode(0, head)
fast, slow = dummy, dummy
for _ in range(n):
fast = fast.next
while fast.next:
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dummy.next
head = [1, 2, 3, 4, 5]
n = 2
head = ListNode.create(head)
print(head) # 1 -> 2 -> 3 -> 4 -> 5
print(removeNthFromEnd(head, n)) # 1 -> 2 -> 3 -> 5
141. Linked List Cycle¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: hash table, linked list, two pointers
- Determine if a linked list has a cycle in it.
graph LR
A((3)) --> B((2))
B --> C((0))
C --> D((4))
graph LR
A((3)) --> B((2))
B --> C((0))
C --> D((4))
D --> B
141. Linked List Cycle - Python Solution
from typing import Optional
from template import ListNode
def hasCycle(head: Optional[ListNode]) -> bool:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
print(hasCycle(ListNode.create([3, 2, 0, -4]))) # False
print(hasCycle(ListNode.create([3, 2, 0, -4], 1))) # True
141. Linked List Cycle - C++ Solution
#include <iostream>
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
bool hasCycle(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (fast == slow) return true;
}
return false;
}
};
23. Merge k Sorted Lists¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: linked list, divide and conquer, heap priority queue, merge sort
- Prerequisite: 21. Merge Two Sorted Lists
- Video explanation: 23. Merge K Sorted Lists - NeetCode
23. Merge k Sorted Lists - Python Solution
import copy
import heapq
from typing import List, Optional
from template import ListNode
# Divide and Conquer
def mergeKListsDC(lists: List[Optional[ListNode]]) -> Optional[ListNode]:
if not lists or len(lists) == 0:
return None
def mergeTwo(l1, l2):
dummy = ListNode()
cur = dummy
while l1 and l2:
if l1.val < l2.val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
cur.next = l1 if l1 else l2
return dummy.next
while len(lists) > 1:
merged = []
for i in range(0, len(lists), 2):
l1 = lists[i]
l2 = lists[i + 1] if i + 1 < len(lists) else None
merged.append(mergeTwo(l1, l2))
lists = merged
return lists[0]
# Heap - Merge k Sorted
def mergeKLists(lists: List[Optional[ListNode]]) -> Optional[ListNode]:
dummy = ListNode()
cur = dummy
minHeap = [] # (val, idx, node)
for idx, head in enumerate(lists):
if head:
heapq.heappush(minHeap, (head.val, idx, head))
while minHeap:
_, idx, node = heapq.heappop(minHeap)
cur.next = node
cur = cur.next
node = node.next
if node:
heapq.heappush(minHeap, (node.val, idx, node))
return dummy.next
n1 = ListNode.create([1, 4, 5])
n2 = ListNode.create([1, 3, 4])
n3 = ListNode.create([2, 6])
lists = [n1, n2, n3]
lists1 = copy.deepcopy(lists)
lists2 = copy.deepcopy(lists)
print(mergeKListsDC(lists1))
# 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6
print(mergeKLists(lists2))
# 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6