Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 82cd834

Browse files
author
openset
committed
Add: Reverse Linked List
1 parent e1c5160 commit 82cd834

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
11
package reverse_linked_list
2+
3+
import . "github.com/openset/leetcode/internal/kit"
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* type ListNode struct {
8+
* Val int
9+
* Next *ListNode
10+
* }
11+
*/
12+
func reverseList(head *ListNode) *ListNode {
13+
var pre *ListNode
14+
for head != nil {
15+
pre, head, head.Next = head, head.Next, pre
16+
}
17+
return pre
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
11
package reverse_linked_list
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
. "github.com/openset/leetcode/internal/kit"
8+
)
9+
10+
type caseType struct {
11+
input []int
12+
expected []int
13+
}
14+
15+
func TestReverseList(t *testing.T) {
16+
tests := [...]caseType{
17+
{
18+
input: []int{1, 2, 3, 4, 5},
19+
expected: []int{5, 4, 3, 2, 1},
20+
},
21+
}
22+
for _, tc := range tests {
23+
output := ListNode2SliceInt(reverseList(SliceInt2ListNode(tc.input)))
24+
if !reflect.DeepEqual(output, tc.expected) {
25+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
26+
}
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /