-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path143.重排链表.go
98 lines (80 loc) · 1.33 KB
/
143.重排链表.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
90
91
92
93
94
95
96
97
/*
* @lc app=leetcode.cn id=143 lang=golang
*
* [143] 重排链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
func main() {
l1 := &ListNode{Val: 1}
l2 := &ListNode{Val: 2}
l3 := &ListNode{Val: 3}
// l4 := &ListNode{Val: 4}
// l5 := &ListNode{Val: 5}
l1.Next = l2
l2.Next = l3
// l3.Next = l4
// l4.Next = l5
print(l1)
// l11 := reverseList(l1)
// print(l11)
reorderList(l1)
print(l1)
}
func print(head *ListNode) {
cur := head
for cur != nil {
fmt.Printf("%d ", cur.Val)
cur = cur.Next
}
fmt.Println()
}
func reorderList(head *ListNode) {
if head == nil {
return
}
cur := head
next := head
for next != nil && next.Next != nil {
cur = cur.Next
next = next.Next.Next
}
newHead := reverseList(cur.Next)
cur.Next = nil
cur1 := head
cur2 := newHead
var tmp1, tmp2 *ListNode
for cur1 != nil && cur2 != nil {
tmp1 = cur1.Next
tmp2 = cur2.Next
cur1.Next = cur2
cur2.Next = tmp1
cur1 = tmp1
cur2 = tmp2
}
}
func reverseList(head *ListNode) *ListNode {
var prev *ListNode = nil
var tmp *ListNode
cur := head
for cur != nil {
tmp = cur.Next
cur.Next = prev
prev = cur
cur = tmp
}
return prev
}
// @lc code=end