All a linked list really is is the head node (which points to the next node, and so on).
To reverse a linked list, just need to remember to store next and update prev and curr pointers at each iteration:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
To recursively reverse:
base case is null node or null node.next
remember to set head.next.next to head and then head.next = None
Deleting a node n is just setting the references to skip n: prev.next = n.next;
Can also delete a curr node even without a prev pointer: curr.data = curr.next.data; curr.next = curr.next.next; Basically, copy (and overwrite) next's data to curr and delete next.
Just make sure to check for the null pointer (!!) and be aware of the head pointer.
Linked lists offer easy modification but non-existent random access.
An important technique is the "runner" one:
Have two pointers iterating through the list, with one pointer either ahead by a fixed amount or actually moving faster.
For example, to determine the midpoint of a linked list, have two pointers such that one of them jumps 2 nodes at once and the other just 1. When the fast pointer hits the end, the slow one will be in the middle of the list.
To determine if a linked list has a cycle, do the same thing where 1 pointer moves ahead 2 nodes at a time. If there's a cycle, the runner will eventually equal the normal curr node. If not, will hit null node.
then, once fast = slow, reset slow to head of list, and move forward both pointers 1 at a time. once they equal again, that's the start of the cycle. then move the fast pointer 1 at a time, once they equal for the 3rd time, that's the length of the cycle
can also use this to detect duplicates in an array given certain constraints
No comments