In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to copy leetcode with random pointers linked list, I believe that most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Given a linked list, each node contains an additional random pointer that can point to any node or empty node in the linked list.
Requires that a deep copy of this linked list be returned.
Example:
Enter:
{"$id": "1", "next": {"$id": "2", "next": null, "random": {"$ref": "2"}, "val": 2}, "random": {"$ref": "2"}, "val": 1}
Explanation:
Node 1 has a value of 1, and its next pointer and random pointer both point to node 2.
Node 2 has a value of 2, whose next pointer points to null and a random pointer to itself.
Tip:
You must return a copy of the given header as a reference to the clone list.
Problem solving skills:
1. Because of the existence of the random pointer, how to locate the random in copy is a problem, so the simple method is to insert an element after each position in the original linked list.
2. Since random may point to the previous pointer, it cannot be disassembled until the copy is finished.
3. Pay attention to the boundary conditions. for pointer problems, be sure to judge the null situation.
/ * / / Definition for a Node.class Node {public: int val; Node* next; Node* random
Node () {}
Node (int _ val, Node* _ next, Node* _ random) {val = _ val; next = _ next; random = _ random;}}; * / class Solution {public: Node* copyRandomList (Node* head) {Node* hull null; Node* tweak null; Node* cur=NULL; cur=head; while (curving null) {tasking null) {tasking null) Cur- > next=new Node (cur- > val,t,NULL); cur=t;} cur=head; if (cur==NULL) {return NULL;} hcurl-> next; tweak; while (curving null) {if (cur- > invalid null) {t-> random=cur- > random- > next } cur=t- > next; if (curving null) {tincture-> next;}} cur=head; hobby-> next; tonal; while (curving null) {cur- > next=t- > next; cur=cur- > next If (curvilinear null) {t-> next=cur- > next; tasking-> next;}}
Return h;}}; these are all the contents of the article "how to copy linked lists with random pointers in leetcode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.