-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path143.go
More file actions
50 lines (49 loc) · 723 Bytes
/
143.go
File metadata and controls
50 lines (49 loc) · 723 Bytes
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
package main
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reorderList(head *ListNode) {
n := 0
tail := new(ListNode)
var p *ListNode
p = head
for p != nil {
tail.Val = p.Val
p = p.Next
if p == nil {
break
}
tmp := new(ListNode)
tmp.Next = tail
tail = tmp
n++
}
if n < 2 {
return
}
direction := 0
relist := new(ListNode)
var q *ListNode
q = relist
p = head
for i := 0; i <= n; i++ {
if direction == 0 {
q.Val = p.Val
p = p.Next
} else {
q.Val = tail.Val
tail = tail.Next
}
if i == n {
break
}
q.Next = new(ListNode)
q = q.Next
direction = 1 - direction
}
head.Next = relist.Next
}