Problem solving & practice
Section 1 of 3
Detect if a cycle exists in a linked list using two pointers
Reverse pointer direction of the list
Combine two increasing ordered lists
Many linked list problems reduce to a handful of reusable pointer movement patterns. Master these and most interview questions become mechanical applications rather than puzzles.
Maintain slow and fast pointers advancing at different speeds
slow = head; fast = head; while(fast && fast.next){ slow = slow.next; fast = fast.next.next; }Iteratively reverse next pointers to flip list direction
prev=null; cur=head; while(cur){ nxt=cur.next; cur.next=prev; prev=cur; cur=nxt; }Select smaller head from two sorted lists repeatedly
while(a && b){ if(a.val<b.val){ tail.next=a; a=a.next; } else { tail.next=b; b=b.next;} tail=tail.next; }After meet, reset one pointer to head and move both 1 step to find entry
while(fast&&fast.next){ slow=slow.next; fast=fast.next.next; if(slow===fast){ slow=head; while(slow!==fast){ slow=slow.next; fast=fast.next;} break; }}Divide list into two based on condition, then recombine
before/after lists; iterate nodes; append to bucket; join
Use middle discovery then modify second half then reconnect
mid via slow/fast; reverse second; weave first & reversed
Single pass using slow/fast pointers
Skip equal neighbors in sorted list
Use length difference trick
Reverse second half & compare
Digit-by-digit addition with carry
Interleave nodes then split