Skip to content

Commit ced980c

Browse files
aQuaaQua
aQua
authored and
aQua
committed
finish Problem 24
完善了README,给程序添加了注释
1 parent ab265bc commit ced980c

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

Algorithms/0024.swap-nodes-in-pairs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Given 1->2->3->4, you should return the list as 2->1->4->3.
99
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
1010

1111
## 解题思路
12-
12+
利用递归,可以简介明了完成程序。详见程序注释。
1313

1414
## 总结
1515

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

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

14-
t := head.Next
15-
head.Next = swapPairs(t.Next)
16-
t.Next = head
14+
// 让temp指向head.Next节点
15+
temp := head.Next
16+
// 让head.Next指向转换好了temp.Next节点
17+
head.Next = swapPairs(temp.Next)
18+
// 让temp.Next指向head节点
19+
temp.Next = head
20+
// temp成为新的head节点
1721

18-
return t
22+
return temp
1923
}

0 commit comments

Comments
 (0)