Intersection of Two Linked Lists:Switch Heads to Align, Not Matching Values
Two pointers that each walk one list then the other meet at the first shared node. A HashSet of nodes is honest extra space; comparing val is not an intersection.
Read More17 questions
Two pointers that each walk one list then the other meet at the first shared node. A HashSet of nodes is honest extra space; comparing val is not an intersection.
Read MoreDetect a cycle with slow and fast on next. A HashSet of values is the wrong test; a HashSet of nodes is honest extra space.
Read MoreWalk both lists behind a dummy head and always splice the smaller current node. Copying values into an ArrayList builds a new chain, not a merge of the nodes you were given.
Read MoreFast takes two steps and slow takes one; when fast hits null, slow is the second middle. Counting length is two walks; a gap of n is a different two-pointer job.
Read MoreFind the middle, reverse the second half, compare values pairwise. Copying into an ArrayList is honest and spends the extra space the follow-up asked you to drop.
Read MoreBecause the list is already sorted, duplicates sit next to each other — skip current.next when the values match. A HashSet or a copied chain pays extra for a gift the sort already gave you.
Read MoreReverse a singly linked list in one pass by walking prev, current, and next. A new list of copied values is not a reverse of the list you were given.
Read MoreWalk both reverse-digit lists behind a dummy, adding digit plus carry. Parsing into long overflows; BigInteger still throws away the encoding they already gave you.
Read MoreA HashMap from original node to copy wires next and random in linear time. Copying the next chain then searching for each random is quadratic; leaving jumps on originals is not a deep copy.
Read MoreCycle I only asks whether slow and fast collide. The entrance is a second walk: send one pointer back to head, then both take one next until they meet again.
Read MoreKeep an odd tail and an even tail, save evenHead, and join once. Filtering odd values is a different prompt; extra lists of nodes are honest extra space.
Read MoreDummy node plus two pointers n apart unlinks the nth-from-end in one pass. Counting length first is two walks; without a dummy, dropping the head is a special case.
Read MoreFold L0, Ln, L1, Ln-1 by cutting at the first-half tail, reversing the second half, and interleaving. Sorted merge is a different splice; skip the cut and even length cycles.
Read MoreWalk to the node before left (a dummy covers left=1), reverse that many nodes in place, and splice the run back. Painting reversed values onto the same nodes leaves every next link where it was.
Read MoreO(n log n) on a singly list is merge-sort of nodes: cut after the first middle, recurse, splice. Copy-sort-writeback keeps identity and still spends O(n) extra.
Read MoreEvery adjacent pair is a k-group of two: dummy, reverse the pair, advance. Swapping values passes a number test and leaves every next link where it was.
Read MoreRepresentation and operations — not the problem set.