Ten problems that drill the linked-list reversal / pointer-surgery pattern from Lesson 11. Open a problem, try it yourself first, then read how to reach the solution. Each solution here was actually run and tested.
How to practise. Draw the nodes as boxes with arrows and physically redraw the arrows as you step through the code — linked-list bugs are almost always an arrow set in the wrong order (you lose the rest of the list). Two reflexes to build: reach for a dummy head whenever the first node might change, and when reversing, always save
nextbefore you overwritecur.next.
All solutions use this node definition (LeetCode’s ListNode):
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = nextThe 10 problems
- 1Reverse Linked List (LeetCode 206)EasyReverse a singly linked list in place: walk once, flipping each next pointer back to the previous node.arrow_forward
- 2Merge Two Sorted Lists (LeetCode 21)EasyMerge two sorted linked lists: a dummy head plus a tail pointer that always takes the smaller front node.arrow_forward
- 3Swap Nodes in Pairs (LeetCode 24)MediumSwap every two adjacent nodes by rewiring pointers: a dummy head and three pointer moves per pair.arrow_forward
- 4Reverse Linked List II (LeetCode 92)MediumReverse only positions left..right of a linked list in one pass, using a dummy head and head-insertion.arrow_forward
- 5Add Two Numbers (LeetCode 2)MediumAdd two numbers stored as reversed digit lists: walk both lists together, carrying like school addition.arrow_forward
- 6Rotate List (LeetCode 61)MediumRotate a linked list right by k: count the length, join tail to head, and cut the ring at n - k%n.arrow_forward
- 7Remove Duplicates from Sorted List II (LeetCode 82)MediumDelete every value that appears more than once in a sorted list: a dummy head lets prev skip whole runs.arrow_forward
- 8Odd Even Linked List (LeetCode 328)MediumGroup odd-position nodes before even-position nodes: build two chains in one pass, then join them.arrow_forward
- 9Copy List with Random Pointer (LeetCode 138)MediumDeep-copy a list whose nodes also have a random pointer: clone every node first, then wire links via a map.arrow_forward
- 10Reverse Nodes in k-Group (LeetCode 25)HardReverse every block of k nodes, leaving a short tail: check k ahead, flip the block, reconnect the ends.arrow_forward