Linked lists · stretch
Merge two sorted linked lists
a and b are sorted linked lists. Write merge_lists(a, b) returning the head of one sorted list made by relinking their nodes — no new Nodes.
A throwaway starting node (a dummy) saves special-casing the first step: build after it, return dummy.next.
- right answers
- arguments left as they should be
- without build
Already written for you — you can use these
1class Node:2def __init__(self, value, next=None):3self.value = value4self.next = next56def build(values):7head = None8for v in reversed(values):9head = Node(v, head)10return head
Run adds head = head.next after your code, to try it.
Stuck on the idea rather than the code? The Fast and slow, and a dummy head lesson walks through it.