Linked Lists Checkpoint
A review of this module. Skip it if you feel confident; the next module does not need it.
Fix the mistakes
The slips that cost marks in this module. Spot each one, then check the fix.
This checkpoint mixes everything in the module. Decide first which technique fits the clue, then write the loop and trace it on a short list.
Walk and rewire with a dummy node
When a problem changes which nodes stay, put a dummy node before the head. Walk with node, and look one step ahead at node.next so you can skip it.
def remove_below(head, low): dummy = Node(0, head) node = dummy while node.next: if node.next.value < low: node.next = node.next.next else: node = node.next return dummy.nextExample 1 · Keep passing marks
class Node: def __init__(self, value, next=None): self.value = value self.next = next
def build(values): head = None for value in reversed(values): head = Node(value, head) return head
def to_list(head): out = [] node = head while node: out.append(node.value) node = node.next return out
def remove_below(head, low): dummy = Node(0, head) node = dummy while node.next: if node.next.value < low: node.next = node.next.next else: node = node.next return dummy.next
print(to_list(remove_below(build([35, 82, 40, 91, 28]), 40)))[82, 40, 91]
What this code does
- Marks below 40 are skipped, including the head 35. The dummy node handles the head without extra code.
- The remaining nodes keep their order.
Return dummy.next, not head: the head may be gone. Move node only when nothing was removed. Time is O(n) and space is O(1).
Two pointers: a gap or two speeds
Two pointers a fixed gap apart find a node counted from the end. Two pointers at different speeds find the middle or a cycle. Both need one pass and no extra memory.
def kth_from_end(head, k): lead = trail = head for _ in range(k): lead = lead.next while lead: lead = lead.next trail = trail.next return trail.valueExample 2 · Value third from the end
class Node: def __init__(self, value, next=None): self.value = value self.next = next
def build(values): head = None for value in reversed(values): head = Node(value, head) return head
def kth_from_end(head, k): lead = trail = head for _ in range(k): lead = lead.next while lead: lead = lead.next trail = trail.next return trail.value
head = build([10, 20, 30, 40, 50, 60])print(kth_from_end(head, 3), kth_from_end(head, 1))40 60
What this code does
- The 3rd from the end of six nodes is 40, and the 1st from the end is the last node, 60.
- One pass and O(1) space: no length is counted.
After k steps, lead is k nodes ahead. When it walks off the end, trail stands on the k-th node from the end.
Choose the technique from the clue
A change at the head, or removing nodes, points to a dummy node. The middle, a cycle or a count from the end points to two pointers. Reversing, or comparing the ends, points to in-place reversal.
Example 3 · Group odd and even positions
class Node: def __init__(self, value, next=None): self.value = value self.next = next
def build(values): head = None for value in reversed(values): head = Node(value, head) return head
def to_list(head): out = [] node = head while node: out.append(node.value) node = node.next return out
def odd_even(head): if head is None: return None odd, even = head, head.next even_head = even while even and even.next: odd.next = even.next odd = odd.next even.next = odd.next even = even.next odd.next = even_head return head
print(to_list(odd_even(build([1, 2, 3, 4, 5, 6]))))print(to_list(odd_even(build([1, 2, 3, 4, 5]))))[1, 3, 5, 2, 4, 6] [1, 3, 5, 2, 4]
What this code does
- Nodes in positions 1, 3, 5 come first, then those in positions 2, 4, 6. Order inside each group is kept.
- Only links change, so it is O(n) time and O(1) space.
Module quiz
Questions from the whole module. Your first answer is your score.
Problems
Graded easy to hard, using everything from this module.
Try each one yourself first. Use the hint if you get stuck. Mark it as done when it works.
Exercise 1· Easy
Remove all nodes holding an even number from [1, 2, 3, 4, 5, 6] and print the rest.
1 3 5
Exercise 2· Easy
Print the sum of all values in the list [12, 8, 20, 5] by walking the nodes.
45
Exercise 3· Easy
Insert 6 at the front of [7, 8, 9] and append 10 at the end. Print the list.
6 7 8 9 10
Exercise 4· Medium
Print the middle value of [3, 6, 9, 12, 15, 18, 21].
12
Exercise 5· Medium
Print the list [5, 4, 3, 2, 1] reversed, as values separated by spaces.
1 2 3 4 5
Exercise 6· Medium
Print the value that is 2nd from the end in [11, 22, 33, 44, 55] using two pointers.
44
Exercise 7· Hard
Merge [2, 6, 10] and [1, 7] (both sorted) and print the merged list.
1 2 6 7 10
Exercise 8· Hard
Print YES if [2, 5, 9, 5, 2] is a palindrome list and NO otherwise. Do not copy the values to a list.
YES
Interview questions
Questions interviewers ask on this topic.
Finish up
Mark complete & continue
Next: Must-do problems: Linked Lists