Posts

Showing posts with the label Linked List

Flattening the Tree into Double linked list

Image
Problem Given the binary tree as shown below: Figure 1. Binary Tree Convert the tree into a double-linked list, where the left pointer and right pointer become the prev and next pointer respectively. Your algorithm should produce the double-linked list as below:

Copying linked list with random pointers

Image
Problem Having a home defined linked list with the following structure, where the next will point to the node in the list and the random will point to a random node in the list. Create a copy of the structure(the data field in each node is not unique for different nodes. As shown Figure 1 and Figure 2, adding an duplicate node between existing nodes in the list can be done in O(N) time. Figure 1. Original linked list Figure 2. After adding duplicate nodes.