-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution35.java
More file actions
38 lines (37 loc) · 988 Bytes
/
Copy pathsolution35.java
File metadata and controls
38 lines (37 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package nowcoder;
public class solution35 {
public RandomListNode Clone(RandomListNode pHead)
{
if(pHead==null)
{
return null;
}
RandomListNode cur = pHead;
while(cur!=null)
{
RandomListNode clone = new RandomListNode(cur.label);
clone.next= cur.next;
cur.next = clone;
cur = clone.next;//指向下一个cur
}
cur = pHead;
while(cur != null)
{
RandomListNode clone = cur.next;
if(cur.random!=null)
{
clone.random = cur.random.next;//curde random结点的next就是clone的random
}
cur = clone.next;
}
cur = pHead;
RandomListNode cloneHead = pHead.next;
while(cur.next!=null)
{
RandomListNode next = cur.next;
cur.next = next.next;
cur = next;
}
return cloneHead;
}
}