-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyListWithRandomPointer.java
59 lines (51 loc) · 1.49 KB
/
CopyListWithRandomPointer.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 题目:深拷贝一个链表,链表除了含有next指针外,还包含一个random指针,该指针指向字符串中的某个节点或者为空。
* 解题思路:
* 1.第一遍扫描:对每个结点进行复制,把复制出来的新结点插在原结点之后
* 2.第二遍扫描:根据原结点的random,给新结点的random赋值
* 3.第三遍扫描:把新结点从原链表中拆分出来
*/
public class CopyListWithRandomPointer {
public static void main(String[] args) {
}
}
class RandomListNode
{
int label;
RandomListNode next,random;
RandomListNode(int x) {this.label=x;}
}
class Solution99
{
public RandomListNode copyRandomList(RandomListNode head)
{
if(head==null) return head;
//第一遍扫描:对每个结点进行复制,把复制出来的新结点插在原结点之后
RandomListNode cur=head;
while(cur!=null)
{
RandomListNode newNode=new RandomListNode(cur.label);
newNode.next=cur.next;
cur.next=newNode;
cur=newNode.next;
}
//第二遍扫描:根据原结点的random,给新结点的random赋值
cur=head;
while(cur!=null)
{
if(cur.random!=null) cur.next.random=cur.random.next;
cur=cur.next.next;
}
//第三遍扫描:把新结点从原链表中拆分出来
RandomListNode newHead=head.next;
cur=head;
while(cur!=null)
{
RandomListNode newNode=cur.next;
cur.next=newNode.next;
if(newNode.next!=null) newNode.next=newNode.next.next;
cur=cur.next;
}
return newHead;
}
}