Problem Names Solution Writeups Code
Reverse Linked List

The idea is that we first create a ListNode instance, then node->next = head->val, then we update head to head->next. Loop until head->next is null.

Merge Two Linked Lists

Simple like merging two sorted array, except that we need to create next pointer and update curr->next to that pointer and increase curr to curr->next.

Reorder List

Create a stack to store ListNode pointer so that we have access from back to front. To maintain the while loop, we need to ensure that the current size of the stack is >= current size of the list that is newly constructed.

Remove n-th Node From End of List

Create a vector and store each pointer inside the vector to get access to all nodes. Then delete the nodes correspondingly.

Copy List With Random Pointer

Create a hash map that maps node pointer of original list to node pointer of new list in the first pass. In the second pass, copy the relation of next and random to the new nodes.

Add Two Numbers

Take care of carry bit and sum bit. To ensure there is no leading zero, create another pointer that points to the second last and then set its next to NULL to prevent leading zero if required.

Linked List Cycle

Floyd algorithm is used to find the starting node of a cycle. The algorithm consists of two passes. In the first pass, create a fast and slow pointer, fast will jump twice while slow will jump once. When fast == slow, break the loop. Then by holding the current slow node pointer, create another slow pointer that starts jumping from head. In this second pass, both slow pointers will only jump once and then stop the loop when slow == slow2, this indicates a cycle is detected. If this is not the case, the linked list will reach its end/tail and no cycle is detected.

Find the Duplicate Number

Since the size of the array is n + 1, and the range for each number inside is [1, n]. We treat array[0] as the head of a linked list, such that array[i] will points to the array[i]th node. Try to find the start of the cycle using Floyd algorithm and this will be the answer. O(n) TC and O(1) space. Great!

LRU Cache

To be able to keep track of least used and most recently used cache, create a doubly linked list (with prev and next), and keep two pointers (head and tail) where head will point to the head of the doubly linked list and tail will point to the last element, note that head and tail pointer can be not used, so actual linked list will be (n+2) size.

Merge k Sorted Lists

Create a function that merge two lists, then do this for each two lists in the array of list. Overwrite lists[i+1] to save memory.

Reverse Nodes in k-Group

First create a reverse function that will reverse a given linked list. Then for each k nodes, select them out and reverse it. The main idea is to connect each segment. To do so, create a curr_prev pointer that will point to the curr_last node after reversed. Connect it to the next kth node if found, else simply connect to curr->next because there is no more node available. This will be an O(n) solution.