Leetcode: 138. Copy List with Random Pointer

Question:

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.

Solution1:

public class Solution {
	HashMap<RandomListNode, RandomListNode> visited = new HashMap<RandomListNode, RandomListNode>();
	// method for the mapping of old and new nodes
	// the parameter is the old node, and the return value is the corresponding cloned new node
	private RandomListNode clonedNode(RandomListNode node) {
		if (node != null) {
			// if the cloned node is already created
			if (visited.containsKey(node)) {
				return visited.get(node);
			}
			// if the cloned node has not been created yet
			else {
				// create the node and put it into the map
				visited.put(node, new RandomListNode(node.label));
				return visited.get(node);
			}
		}
		return null;
	}
	
	public RandomListNode copyRandomList(RandomListNode head) {
		if (head == null) {
			return null;
		}

		// if head != null, create a new head
		RandomListNode newHead = new RandomListNode(head.label);
		RandomListNode oldCur = head, newCur = newHead;
		while (oldCur != null) {
			newCur.next = clonedNode(oldCur.next);
			newCur.random = clonedNode(oldCur.random);
			newCur = newCur.next;
			oldCur = oldCur.next;
		}
	}
	return newHead;
}

Note:

  1. To create a cloned list, is actually creating a mapping from an old list to a new list. (To create a map, we can use a hashmap directly, instead of using the index or the sequence of the nodes in the list.)
  2. To avoid the duplication of new nodes, before creating new nodes, we check if it already exists.
  3. Time Complexity : O(N)O(N) because we make one pass over the original linked list.
  4. Space Complexity : O(N)O(N) as we have a dictionary containing mapping from old list nodes to new list nodes. Since there are NN nodes, we have O(N)O(N) space complexity. Space Complexity : O(N)O(N) as we have a dictionary containing mapping from old list nodes to new list nodes. Since there are NN nodes, we have O(N)O(N) space complexity.

Solution2:

tweak the original linked list and keep every cloned node next to its original node.

public class Solution {
	public RandomListNode copyRandomList(RandomListNode head) {
		if (head == null) {
			return null;
		}
		//tweak the original linked list and keep every cloned node next to its original node.
		RandomListNode cur = head;
		while (cur != null) {
			RandomListNode newNode = new RandomListNode(cur.label);
			newNode.next = cur.next;
			cur.next = newNode;
			cur = newNode.next;
		}
		// find random node list for the new nodes 
		cur = head;
		while (cur != null) {
			cur.next.random = (cur.random == null) ? null : cur.random.next;
			cur = cur.next.next;
		}
		// seperate the tweaked nodes
		RandomListNode oldCur = head, newCur = head.next, newHead = head.next;
		while (oldCur != null) {
			oldCur.next = oldCur.next.next;
			newCur.next = (newCur.next == null) ? null : newCur.next.next;
			oldCur = oldCur.next;
			newCur = newCur.next;
		}
		return newHead;
	}
}

Notes:

  1. Don’t modify the input parameter
  2. Time Complexity : O(N)
  3. Space Complexity : O(1)

Solution3: Recursion

public class Solution {
	HashMap<RandomListNode, RandomListNode> visited = new HashMap<RandomListNode, RandomListNode>();
	public RandomListNode copyRandomList(RandomListNode head) {
		if (head == null) {
			return null;
		}
		if (visited.containsKey(head)) {
			return visited.get(head);
		}
		RandomListNode node = new RandomListNode(head.label);
		visited.add(head,node);
		head.next = copyRandomList(head.next);
		head.random = copyRandomList(head.random);
		return node;
		
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值