Java Algorithms: Copying List with Random Pointer (LeetCode)
This story was originally published on HackerNoon at: https://hackernoon.com/java-algorithms-copying-list-with-random-pointer-leetcode.
A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.
Check more stories related to programming at: https://hackernoon.com/c/programming. You can also check exclusive content about #programming, #java-programming, #leetcode, #linked-lists, #programming-tips, #coding, #coding-challenge, #debugging, #hackernoon-es, and more.
This story was written by: @rakhmedovrs. Learn more about this writer by checking @rakhmedovrs's about page, and for more stories, please visit hackernoon.com.
Let’s start from solving the first subproblem. Let’s do simple thing iterate through linked list and create a copy for every node in it. I choose HashMap for storing pairs like old node -> new node. As long as we’re going to iterate from head of the list to tail let’s also introduce temp variable and set it to be the head of the list.
You May Also Like

Artificial Intelligence Masterclass
AI Masterclass

AI Fire Daily
AIFire.co

Acquired
Ben Gilbert and David Rosenthal

Barış Özcan ile 111 Hz
Podbee Media

Jack Hopkins
Jack Hopkins

Jak Piggott
Jak Piggott

LINUX Unplugged
Jupiter Broadcasting

Theo - t3․gg
Theo - t3․gg

Scaling Postgres
Creston Jamison

Community discussion