Skip to content

Commit deaa100

Browse files
aQuaaQua
aQua
authored and
aQua
committed
adding Problem 24
能够通过测试,但是程序啰嗦了
1 parent 2f7b374 commit deaa100

File tree

3 files changed

+96
-7
lines changed

3 files changed

+96
-7
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# [24. Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)
22

33
## 题目
4+
Given a linked list, swap every two adjacent nodes and return its head.
45

6+
For example,
7+
Given 1->2->3->4, you should return the list as 2->1->4->3.
8+
9+
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
510

611
## 解题思路
712

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
11
package Problem0024
22

3+
// ListNode ListNode
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
func swapPairs(head *ListNode) *ListNode {
10+
if head == nil || head.Next == nil {
11+
return head
12+
}
13+
14+
var cur *ListNode
15+
head = swap(head)
16+
cur = head.Next
17+
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
35+
}
Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0024
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -15,13 +15,13 @@ type question struct {
1515
// para 是参数
1616
// one 代表第一个参数
1717
type para struct {
18-
one string
18+
one []int
1919
}
2020

2121
// ans 是答案
2222
// one 代表第一个答案
2323
type ans struct {
24-
one string
24+
one []int
2525
}
2626

2727
func Test_Problem0024(t *testing.T) {
@@ -30,17 +30,68 @@ func Test_Problem0024(t *testing.T) {
3030
qs := []question{
3131

3232
question{
33-
para{""},
34-
ans{""},
33+
para{[]int{}},
34+
ans{[]int{}},
3535
},
36-
36+
37+
question{
38+
para{[]int{1}},
39+
ans{[]int{1}},
40+
},
41+
42+
question{
43+
para{[]int{1, 2, 3, 4}},
44+
ans{[]int{2, 1, 4, 3}},
45+
},
46+
47+
question{
48+
para{[]int{1, 2, 3, 4, 5}},
49+
ans{[]int{2, 1, 4, 3, 5}},
50+
},
51+
3752
// 如需多个测试,可以复制上方元素。
3853
}
3954

4055
for _, q := range qs {
4156
a, p := q.ans, q.para
4257
fmt.Printf("~~%v~~\n", p)
4358

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
59+
ast.Equal(a.one, l2s(swapPairs(s2l(p.one))), "输入:%v", p)
60+
}
61+
}
62+
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+
67+
// convert *ListNode to []int
68+
func l2s(head *ListNode) []int {
69+
res := []int{}
70+
71+
for head != nil {
72+
res = append(res, head.Val)
73+
head = head.Next
4574
}
75+
76+
return res
77+
}
78+
79+
// convert []int to *ListNode
80+
func s2l(nums []int) *ListNode {
81+
if len(nums) == 0 {
82+
return nil
83+
}
84+
85+
res := &ListNode{
86+
Val: nums[0],
87+
}
88+
temp := res
89+
for i := 1; i < len(nums); i++ {
90+
temp.Next = &ListNode{
91+
Val: nums[i],
92+
}
93+
temp = temp.Next
94+
}
95+
96+
return res
4697
}

0 commit comments

Comments
 (0)