Skip to content

Commit 029a8b4

Browse files
aQuaaQua
aQua
authored and
aQua
committed
finish Problem 24
添加了一个简介明了的解答
1 parent deaa100 commit 029a8b4

File tree

2 files changed

+4
-24
lines changed

2 files changed

+4
-24
lines changed

Algorithms/0024.swap-nodes-in-pairs/swap-nodes-in-pairs.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,9 @@ func swapPairs(head *ListNode) *ListNode {
1111
return head
1212
}
1313

14-
var cur *ListNode
15-
head = swap(head)
16-
cur = head.Next
14+
t := head.Next
15+
head.Next = swapPairs(t.Next)
16+
t.Next = head
1717

18-
for cur != nil && cur.Next != nil {
19-
cur.Next = swap(cur.Next)
20-
cur = cur.Next.Next
21-
}
22-
23-
return head
24-
}
25-
26-
func swap(l *ListNode) *ListNode {
27-
if l.Next != nil {
28-
temp := l.Next
29-
l.Next = temp.Next
30-
temp.Next = l
31-
return temp
32-
}
33-
34-
return l
18+
return t
3519
}

Algorithms/0024.swap-nodes-in-pairs/swap-nodes-in-pairs_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ func Test_Problem0024(t *testing.T) {
6060
}
6161
}
6262

63-
func Test_swap(t *testing.T) {
64-
assert.Equal(t, []int{2, 1, 3, 4}, l2s(swap(s2l([]int{1, 2, 3, 4}))), "swap没能成功转换")
65-
}
66-
6763
// convert *ListNode to []int
6864
func l2s(head *ListNode) []int {
6965
res := []int{}

0 commit comments

Comments
 (0)