138. Copy List with Random Pointer

202408011719
tags: #linked-list

var copyRandomList = function(head) {
	let cur = head;
	const map = new Map();
	while (cur !== null) {
		map.set(cur, new Node(cur.val));
		cur = cur.next;
	}
	cur = head;
	while (cur !== null) {
		const newNode = map.get(cur);
		newNode.next = map.get(cur.next) ?? null;
		newNode.random = map.get(cur.random) ?? null;
		cur = cur.next;
	}
	return map.get(head);
};

Reference

https://leetcode.com/problems/copy-list-with-random-pointer