Chào mừng bạn đến blog Ynghialagi.com Trang Chủ

Table of Content

Bài đăng

Copy linked list with arbitrary pointer - leetcode Mới nhất

Thủ Thuật Hướng dẫn Copy linked list with arbitrary pointer – leetcode Mới Nhất


Pro đang tìm kiếm từ khóa Copy linked list with arbitrary pointer – leetcode được Update vào lúc : 2022-12-06 15:10:07 . Với phương châm chia sẻ Mẹo Hướng dẫn trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi Read tài liệu vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Admin lý giải và hướng dẫn lại nha.


LeetCode Copy List with Random Pointer


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.


Nội dung chính


    LeetCode Copy List with Random PointerRelated posts:

Return a deep copy of the list.


Java Solution 1


We can solve this problem by doing the following steps:


copy every node, i.e., duplicate every node, and insert it to the listcopy random pointers for all newly created nodesbreak the list to twopublic 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 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<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;



LeetCode Solution Sort a linked list using insertion sort in JavaLeetCode Remove Nth Node From End of List (Java)LeetCode Linked List Random Node (Java)LeetCode Reorder List (Java)Category >> Algorithms


Share Link Download Copy linked list with arbitrary pointer – leetcode miễn phí


Bạn vừa tìm hiểu thêm tài liệu Với Một số hướng dẫn một cách rõ ràng hơn về Clip Copy linked list with arbitrary pointer – leetcode tiên tiến và phát triển nhất Chia SẻLink Download Copy linked list with arbitrary pointer – leetcode Free.



Giải đáp vướng mắc về Copy linked list with arbitrary pointer – leetcode


Nếu sau khi đọc nội dung bài viết Copy linked list with arbitrary pointer – leetcode vẫn chưa hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Mình lý giải và hướng dẫn lại nha

#Copy #linked #list #arbitrary #pointer #leetcode

Đăng nhận xét