Singly linked list
easy 18 minA singly linked list is a chain of nodes where each node holds a value and a pointer to the next node.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What is the time complexity of inserting at the head of a singly linked list?
Why can't we do binary search on a linked list?
Which pattern finds the middle of a linked list in one pass?
What is the extra space complexity of reversing a linked list iteratively?
A singly linked list keeps both a head and a tail pointer. Which operation is still O(n)?
Start with 10 -> 20 -> 30. Run insert_head(5), then delete_at(2), then insert_tail(40). What is the list?
In a reverse loop you write current.next = prev and then current = current.next. What happens?
You want to insert at index 3. How many times should the walking loop advance current from the head?
Running slow and fast pointers on 10 -> 20 -> 30 -> 40 -> 50 -> 60 with while fast and fast.next, where does slow stop?
Floyd's algorithm runs on a list and fast becomes None. What can you conclude?
Pick an answer to see why.
Not quite. Head insertion only updates two pointers, so it takes O(1) whatever the length.
Not quite. Binary search needs to jump to the middle in O(1). A linked list has to walk there from the head.
Not quite. Slow moves one step and fast moves two. When fast reaches the end, slow is at the middle.
Not quite. Only three pointers (prev, current, next) are used, however long the list is.
Not quite. Deleting the tail needs the node before it, and there is no way back from the tail. Walking to it is O(n).
Not quite. insert_head(5) gives 5 -> 10 -> 20 -> 30. Index 2 is 20, so deleting it gives 5 -> 10 -> 30. Appending 40 gives 5 -> 10 -> 30 -> 40.
Not quite. After the first line current.next is prev, so the second line moves backwards. The original next node was never saved.
Not quite. You stop on the node before the target, index 2, which takes pos - 1 = 2 steps from the head.
Not quite. slow visits 20, 30, 40 while fast visits 30, 50, then None. With an even length this loop returns the second middle, 40.
Not quite. In a list with a cycle no pointer ever reaches None. Reaching the end proves the list is cycle-free.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
What does each node in a singly linked list hold?
A value and one pointer, next, to the following node. The last node's next is None.
Say your answer, then reveal it.
Spell out the LINK acronym.
Linear chain of nodes, Index by traversal only, Next pointer to the following node, Knows only its neighbour.
Say your answer, then reveal it.
Why is insert at the head O(1)?
Only two assignments: new.next = head, then head = new. No traversal and nothing shifts.
Say your answer, then reveal it.
Why is delete tail O(n), even with a tail pointer?
You must find the second-to-last node to set its next to None, and a singly linked list cannot step backwards.
Say your answer, then reveal it.
To insert or delete at position k, which node do you stop on?
The node at k - 1, the one before the target, because its next pointer is the one that changes.
Say your answer, then reveal it.
Finish the rhyme: "Save the next before you switch..."
"...or you'll lose your list in a glitch." Save current.next before overwriting it.
Say your answer, then reveal it.
The four moves of iterative reverse?
Save next, reverse the link, advance prev, advance current. When current is None, prev is the new head.
Say your answer, then reveal it.
Why can't you binary search a linked list?
There is no random access: reaching the middle takes n/2 hops, which already costs more than a linear scan saves.
Say your answer, then reveal it.
What do slow and fast pointers solve?
Find the middle, detect a cycle (Floyd's tortoise and hare), find the nth node from the end, all in one pass with O(1) space.
Say your answer, then reveal it.
What is a dummy node for?
A fake node before the head so the head is not a special case. Return dummy.next at the end.
Say your answer, then reveal it.
Which picture makes a singly linked list stick?
A train: the engine is the head, each coupler is a next pointer, and to reach car 5 you walk through cars 1 to 4.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.