Clone a Linked List with the next and random pointer

  • Firstly, we will create the copy of node1 and then add or insert it between node1 and node2 in the ori Linked List. After that, we will create a copy of node2 and add it between node2 and node3 in ori and so on, add the copy of N after the nth node.
  • Next, we will copy the given arb(arbitrary) link in the below fashion.
  • Now, we restore the ori linked list and copy linked list in the below fashion in a single loop.
  • Then we are required to confirm the ori->next is null. If it null, return the cloned list.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store