-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswap-nodes-in-pairs.go
77 lines (70 loc) · 1.42 KB
/
swap-nodes-in-pairs.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
package list
// https://leetcode-cn.com/problems/swap-nodes-in-pairs/
// Time: O(n)
// Space: O(1), but it not really swap the nodes, only replace the value.
func swapPairs(head *ListNode) *ListNode {
if head == nil {
return nil
}
p, q := head, head.Next
for p != nil && q != nil {
p.Val, q.Val = q.Val, p.Val
p = q.Next
if p != nil {
q = p.Next
}
}
return head
}
// If we must to swap both neighbour nodes, how to do it?
func swapPairs2(head *ListNode) *ListNode {
if head == nil {
return nil
}
p, q := head, head.Next
var last *ListNode // record last already swapped pointer.
for p != nil && q != nil {
p.Next = q.Next
q.Next = p
if last != nil {
last.Next = q
} else {
head = q
}
last = p
// after swap
p = p.Next
if p != nil {
q = p.Next
}
}
return head
}
// Simplify code with dummy head.
func swapPairs3(head *ListNode) *ListNode {
if head == nil {
return nil
}
dummy := &ListNode{Next: head}
p := dummy // record the last swapped pointer
for p.Next != nil && p.Next.Next != nil {
node1 := p.Next
node2 := p.Next.Next
// swap
node1.Next = node2.Next
node2.Next = node1
p.Next = node2
p = node1 // tail of last swapped pointer
}
return dummy.Next
}
// Recursively
func swapPairs4(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
newHead := head.Next
head.Next = swapPairs4(newHead.Next)
newHead.Next = head
return newHead
}