Category Archives: Heap

Clone a linked list with next and random pointer

Clone a linked list with next and random pointer

Algorithm:
  • Create a hash map
  • Create new list head pointer and list node
  • Now iterate given list from start to end
    • Create a new list node by just coping data from original list
    • And put original node random node pointer in hash map
  • Now iterate once again original list and update new list random node pointer with reference to map.

Latest Source Code:
Github: ListCloneWithRandomPointer.java


Output:

1 2 3 4 5 
1 2 3 4 5