-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path143. Reorder List.go
89 lines (80 loc) · 1.56 KB
/
143. Reorder List.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
// 解法一 单链表
func reorderList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
// 寻找中间结点
p1 := head
p2 := head
for p2.Next != nil && p2.Next.Next != nil {
p1 = p1.Next
p2 = p2.Next.Next
}
// 反转链表后半部分 1->2->3->4->5->6 to 1->2->3->6->5->4
preMiddle := p1
preCurrent := p1.Next
for preCurrent.Next != nil {
current := preCurrent.Next
preCurrent.Next = current.Next
current.Next = preMiddle.Next
preMiddle.Next = current
}
// 重新拼接链表 1->2->3->6->5->4 to 1->6->2->5->3->4
p1 = head
p2 = preMiddle.Next
for p1 != preMiddle {
preMiddle.Next = p2.Next
p2.Next = p1.Next
p1.Next = p2
p1 = p2.Next
p2 = preMiddle.Next
}
return head
}
// 解法二 数组
func reorderList1(head *ListNode) *ListNode {
array := listToArray(head)
length := len(array)
if length == 0 {
return head
}
cur := head
last := head
for i := 0; i < len(array)/2; i++ {
tmp := &ListNode{Val: array[length-1-i], Next: cur.Next}
cur.Next = tmp
cur = tmp.Next
last = tmp
}
if length%2 == 0 {
last.Next = nil
} else {
cur.Next = nil
}
return head
}
func listToArray(head *ListNode) []int {
array := []int{}
if head == nil {
return array
}
cur := head
for cur != nil {
array = append(array, cur.Val)
cur = cur.Next
}
return array
}