Skip to content

Commit 9e037c6

Browse files
committed
Merge branch 'master' of github.com:halfrost/LeetCode-Go
2 parents 823d42e + 00a1c77 commit 9e037c6

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
|0135|Candy||32.8%|Hard||
276276
|0136|Single Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0136.Single-Number)|66.4%|Easy||
277277
|0137|Single Number II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0137.Single-Number-II)|53.5%|Medium||
278-
|0138|Copy List with Random Pointer||39.5%|Medium||
278+
|0138|Copy List with Random Pointer|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0138.Copy-List-With-Random-Pointer)|39.5%|Medium||
279279
|0139|Word Break||41.4%|Medium||
280280
|0140|Word Break II||34.3%|Hard||
281281
|0141|Linked List Cycle|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0141.Linked-List-Cycle)|42.2%|Easy||
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
//执行用时:0 ms, 在所有 Go 提交中击败了100.00%的用户
4+
//内存消耗:3.4 MB, 在所有 Go 提交中击败了100.00%的用户
5+
type Node struct {
6+
Val int
7+
Next *Node
8+
Random *Node
9+
}
10+
11+
func copyRandomList(head *Node) *Node {
12+
if head == nil {
13+
return nil
14+
}
15+
tempHead := copyNodeToLinkedList(head)
16+
return splitLinkedList(tempHead)
17+
}
18+
19+
func splitLinkedList(head *Node) *Node {
20+
cur := head
21+
head = head.Next
22+
for cur != nil && cur.Next != nil {
23+
cur.Next, cur = cur.Next.Next, cur.Next
24+
}
25+
return head
26+
}
27+
28+
func copyNodeToLinkedList(head *Node) *Node {
29+
cur := head
30+
for cur != nil {
31+
node := &Node{
32+
Val: cur.Val,
33+
Next: cur.Next,
34+
}
35+
cur.Next, cur = node, cur.Next
36+
37+
}
38+
cur = head
39+
for cur != nil {
40+
if cur.Random != nil {
41+
cur.Next.Random = cur.Random.Next
42+
}
43+
cur = cur.Next.Next
44+
}
45+
return head
46+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package leetcode
2+
3+
import "testing"
4+
5+
func Test_Problem138(t *testing.T) {
6+
7+
}

0 commit comments

Comments
 (0)