linked lists Flashcards
1
Q
How do you reverse a linked list? (iterative)
A
Init 3 pointers, null, current, prev. While current is not null, set next to current.next. Set current.next to prev. Set prev to current. Set current to next.
2
Q
How do you reverse a linked list? (recursive)
A
divide list into two parts, head node and rest of list. call reverse for rest of the linkedList. Link the rest to the first.
fix head pointer
3
Q
Find intersection of two linked lists
A
Draw them out, being careful not to make them the same length. Notice that if there is an intersection, they will end on the same node.
- Run through each LL to get lengths and tails
- Compare tails, if different, return immediately
- Set two opinters to start of each LL
- On longer LL, advance its pointer by diff in lengths
- Traverse each LL until pointers are the same.