Mục lục bài viết
Cập Nhật: 2021-12-06 15:06:09,You Cần tương hỗ về Copy linked list with arbitrary pointer – leetcode. Bạn trọn vẹn có thể lại phản hồi ở cuối bài để Admin đc lý giải rõ ràng hơn.
Category: Algorithms December 14, 2012
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.
Tóm lược đại ý quan trọng trong bài
Return a deep copy of the list.
Java Solution 1
We can solve this problem by doing the following steps:
public RandomListNode copyRandomList(RandomListNode head)
if (head == null)
return null;
RandomListNode p. = head;
// copy every node and insert to list
while (p. != null)
RandomListNode copy = new RandomListNode(p..label);
copy.next = p..next;
p..next = copy;
p. = copy.next;
// copy random pointer for each new node
p. = head;
while (p. != null)
if (p..random != null)
p..next.random = p..random.next;
p. = p..next.next;
// break list to two
p. = head;
RandomListNode newHead = head.next;
while (p. != null)
RandomListNode temp = p..next;
p..next = temp.next;
if (temp.next != null)
temp.next = temp.next.next;
p. = p..next;
return newHead;
public RandomListNode copyRandomList(RandomListNode head) if (head == null) return null; RandomListNode p. = head; // copy every node and insert to list while (p. != null) RandomListNode copy = new RandomListNode(p..label); copy.next = p..next; p..next = copy; p. = copy.next; // copy random pointer for each new node p. = head; while (p. != null) if (p..random != null) p..next.random = p..random.next; p. = p..next.next; // break list to two p. = head; RandomListNode newHead = head.next; while (p. != null) RandomListNode temp = p..next; p..next = temp.next; if (temp.next != null) temp.next = temp.next.next; p. = p..next; return newHead;
The break list part above move pointer 2 steps each time, you can also move one at a time which is simpler, like the following:
while(p. != null && p..next != null)
RandomListNode temp = p..next;
p..next = temp.next;
p. = temp;
while(p. != null && p..next != null) RandomListNode temp = p..next; p..next = temp.next; p. = temp;
Java Solution 2 – Using HashMap
From Xiaomeng’s comment below, we can use a HashMap which makes it simpler.
public RandomListNode copyRandomList(RandomListNode head)
if (head == null)
return null;
HashMap<RandomListNode, RandomListNode> map = new HashMap<RandomListNode, RandomListNode>();
RandomListNode newHead = new RandomListNode(head.label);
RandomListNode p. = head;
RandomListNode q = newHead;
map.put(head, newHead);
p. = p..next;
while (p. != null)
RandomListNode temp = new RandomListNode(p..label);
map.put(p., temp);
q.next = temp;
q = temp;
p. = p..next;
p. = head;
q = newHead;
while (p. != null)
if (p..random != null)
q.random = map.get(p..random);
else
q.random = null;
p. = p..next;
q = q.next;
return newHead;
public RandomListNode copyRandomList(RandomListNode head) if (head == null) return null; HashMap map = new HashMap(); RandomListNode newHead = new RandomListNode(head.label); RandomListNode p. = head; RandomListNode q = newHead; map.put(head, newHead); p. = p..next; while (p. != null) RandomListNode temp = new RandomListNode(p..label); map.put(p., temp); q.next = temp; q = temp; p. = p..next; p. = head; q = newHead; while (p. != null) if (p..random != null) q.random = map.get(p..random); else q.random = null; p. = p..next; q = q.next; return newHead;
Category >> Algorithms
– Một số Keywords tìm kiếm nhiều : ” Video full hướng dẫn Copy linked list with arbitrary pointer – leetcode tiên tiến và phát triển nhất , Chia Sẻ Link Down Copy linked list with arbitrary pointer – leetcode “.
Quý quý khách trọn vẹn có thể để lại Comments nếu gặp yếu tố chưa hiểu nhé.
#Copy #linked #list #arbitrary #pointer #leetcode