Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf4a5127

Browse files
author
applewjg
committed
CopyListwithRandomPointer
Change-Id: I9bbb3d92586800a06fa3890b32643be564def72f
1 parent7aa79e5 commitf4a5127

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

‎CopyListwithRandomPointer.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Jan 13, 2015
4+
Problem: Copy List with Random Pointer
5+
Difficulty: Easy
6+
Source: http://oj.leetcode.com/problems/copy-list-with-random-pointer/
7+
Notes:
8+
A linked list is given such that each node contains an additional random pointer
9+
which could point to any node in the list or null.
10+
Return a deep copy of the list.
11+
12+
Solution: 1. HashMap
13+
2. uses constant extra space.
14+
3. Recursive. (Stack)-->StackOverflow in Java.
15+
4. Iterative. (Queue)
16+
*/
17+
/**
18+
* Definition for singly-linked list with a random pointer.
19+
* class RandomListNode {
20+
* int label;
21+
* RandomListNode next, random;
22+
* RandomListNode(int x) { this.label = x; }
23+
* };
24+
*/
25+
publicclassSolution {
26+
publicRandomListNodecopyRandomList_1(RandomListNodehead) {
27+
if (head ==null)returnnull;
28+
HashMap<RandomListNode,RandomListNode>map =newHashMap<RandomListNode,RandomListNode>();
29+
RandomListNodedummy =newRandomListNode(-1);
30+
RandomListNodecurNew =dummy,cur =head;
31+
while (cur !=null) {
32+
if (map.containsKey(cur) ==false) {
33+
map.put(cur,newRandomListNode(cur.label));
34+
}
35+
if (cur.random !=null &&map.containsKey(cur.random) ==false) {
36+
map.put(cur.random,newRandomListNode(cur.random.label));
37+
}
38+
curNew.next =map.get(cur);
39+
curNew.next.random =map.get(cur.random);
40+
curNew =curNew.next;
41+
cur =cur.next;
42+
}
43+
returndummy.next;
44+
}
45+
publicRandomListNodecopyRandomList_2(RandomListNodehead) {
46+
if (head ==null)returnnull;
47+
RandomListNodecur =head;
48+
while (cur !=null) {
49+
RandomListNodenewnode =newRandomListNode(cur.label);
50+
newnode.next =cur.next;
51+
cur.next =newnode;
52+
cur =cur.next.next;
53+
}
54+
cur =head;
55+
while (cur !=null) {
56+
if (cur.random !=null) {
57+
cur.next.random =cur.random.next;
58+
}
59+
cur =cur.next.next;
60+
}
61+
RandomListNodedummy =newRandomListNode(-1);
62+
RandomListNodecurNew =dummy;
63+
cur =head;
64+
while (cur !=null) {
65+
curNew.next =cur.next;
66+
curNew =curNew.next;
67+
cur.next =cur.next.next;
68+
cur =cur.next;
69+
}
70+
returndummy.next;
71+
}
72+
publicRandomListNodecopyRandomList_3(RandomListNodehead) {/*StackOverflowError*/
73+
if (head ==null)returnnull;
74+
HashMap<RandomListNode,RandomListNode>map =newHashMap<RandomListNode,RandomListNode>();
75+
returncopy(head,map);
76+
}
77+
publicRandomListNodecopy(RandomListNoderoot,HashMap<RandomListNode,RandomListNode>map) {
78+
if (root ==null)returnnull;
79+
if (map.containsKey(root) ==true) {
80+
returnmap.get(root);
81+
}
82+
RandomListNodenewnode =newRandomListNode(root.label);
83+
map.put(root,newnode);
84+
newnode.next =copy(root.next,map);
85+
newnode.random =copy(root.random,map);
86+
returnnewnode;
87+
}
88+
publicRandomListNodecopyRandomList_4(RandomListNodehead) {
89+
if (head ==null)returnnull;
90+
HashMap<RandomListNode,RandomListNode>map =newHashMap<RandomListNode,RandomListNode>();
91+
Queue<RandomListNode>queue =newLinkedList<RandomListNode>();
92+
queue.offer(head);
93+
map.put(head,newRandomListNode(head.label));
94+
while (queue.isEmpty() ==false) {
95+
RandomListNodecur =queue.poll();
96+
if (cur.next !=null &&map.containsKey(cur.next) ==false) {
97+
RandomListNodenewnode =newRandomListNode(cur.next.label);
98+
map.put(cur.next,newnode);
99+
queue.offer(cur.next);
100+
}
101+
map.get(cur).next =map.get(cur.next);
102+
if (cur.random !=null &&map.containsKey(cur.random) ==false) {
103+
RandomListNodenewnode =newRandomListNode(cur.random.label);
104+
map.put(cur.random,newnode);
105+
queue.offer(cur.random);
106+
}
107+
map.get(cur).random =map.get(cur.random);
108+
}
109+
returnmap.get(head);
110+
}
111+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp