Module 5 · Stacks, Queues and Linked Lists · Lesson 18 of 40
Linked list tricks
Delete a node, reverse a list, find the middle and catch a cycle with slow and fast pointers. Plus doubly and circular lists.
In this lesson · about 24 minutes
- Reverse a linked list by flipping arrows
- Find the middle node with slow and fast pointers
- Know what doubly and circular linked lists are
Deleting a node
To remove a node, make the node before it skip over it: prev.next = prev.next.next. Deleting the head is special: just move the head forward, head = head.next.
Reversing
Walk the list once and flip each arrow to point backwards. You need three names: prev (the part already reversed), node (the current one) and nxt (so you don't lose the rest).
prev = None
node = head
while node is not None:
nxt = node.next # remember the rest
node.next = prev # flip the arrow
prev = node # move both forward
node = nxt
# prev is the new head
Finding the middle: slow and fast
Send two pointers from the head. Slow moves 1 node at a time; fast moves 2. When fast reaches the end, slow is in the middle. One pass, no counting.
Catching a cycle
If a list loops back on itself, walking it never ends. Use slow and fast again: if there is a loop, fast will eventually lap slow and they meet. If fast reaches None, there is no loop. (This is called Floyd's cycle detection, or "tortoise and hare".)
Other kinds of linked lists
| Kind | Each node has | Good for |
|---|---|---|
| Singly | next | Simple, least memory |
| Doubly | next and prev | Walking both ways; deleting a node you already hold in O(1) |
| Circular | tail.next points back to the head | Taking turns forever (round robin) |
Try it: reverse and find the middle
Exercise
Reverse a linked list
Write reverse(head) that reverses the list by flipping arrows and returns the new head. (build and to_list helpers are ready.)
Code that runs before yours
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def build(values):
head = None
for v in reversed(values):
head = Node(v, head)
return head
def to_list(head):
out = []
while head is not None:
out.append(head.value)
head = head.next
return out
Exercise
Middle value
Write middle(head) that returns the value of the middle node using slow and fast pointers. For an even number of nodes, return the second of the two middle ones. The list is not empty.
Code that runs before yours
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def build(values):
head = None
for v in reversed(values):
head = Node(v, head)
return head
def to_list(head):
out = []
while head is not None:
out.append(head.value)
head = head.next
return out
Exercise
Does it loop?
Write has_cycle(head) that returns True if the list loops back on itself. Use slow and fast pointers: if they ever point at the same node, there is a loop.
Code that runs before yours
class Node:
def __init__(self, value, next=None):
self.value = value
self.next = next
def build(values):
head = None
for v in reversed(values):
head = Node(v, head)
return head
def to_list(head):
out = []
while head is not None:
out.append(head.value)
head = head.next
return out
def looped(values, back_to):
head = build(values)
tail, target, i, node = None, None, 0, head
while node:
if i == back_to:
target = node
tail = node
node = node.next
i += 1
tail.next = target
return head
Check your understanding0 of 3 correct
Quiz score: 0 of 3
Exercises solved: 0 of 3
Noviq Academy is free and for learning. This course follows the topics of a set of DSA study notes, rewritten in Noviq's own words with Python examples. Python runs in your own browser with Pyodide; nothing you type is sent to Noviq.