mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 206
This commit is contained in:
9
Algorithms/206.Reverse-Linked-List/README.md
Normal file
9
Algorithms/206.Reverse-Linked-List/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/)
|
||||
|
||||
## 题目
|
||||
|
||||
Reverse a singly linked list.
|
||||
|
||||
## 题目大意
|
||||
|
||||
翻转单链表
|
||||
26
Algorithms/206.Reverse-Linked-List/Reverse Linked List.go
Normal file
26
Algorithms/206.Reverse-Linked-List/Reverse Linked List.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package leetcode
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
|
||||
// ListNode define
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func reverseList(head *ListNode) *ListNode {
|
||||
var behind *ListNode
|
||||
for head != nil {
|
||||
next := head.Next
|
||||
head.Next = behind
|
||||
behind = head
|
||||
head = next
|
||||
}
|
||||
return behind
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question206 struct {
|
||||
para206
|
||||
ans206
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para206 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans206 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem206(t *testing.T) {
|
||||
|
||||
qs := []question206{
|
||||
|
||||
question206{
|
||||
para206{[]int{1, 2, 3, 4, 5}},
|
||||
ans206{[]int{5, 4, 3, 2, 1}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 206------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans206, q.para206
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, l2s(reverseList(s2l(p.one))))
|
||||
}
|
||||
}
|
||||
|
||||
// convert *ListNode to []int
|
||||
func l2s(head *ListNode) []int {
|
||||
res := []int{}
|
||||
|
||||
for head != nil {
|
||||
res = append(res, head.Val)
|
||||
head = head.Next
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// convert []int to *ListNode
|
||||
func s2l(nums []int) *ListNode {
|
||||
if len(nums) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
res := &ListNode{
|
||||
Val: nums[0],
|
||||
}
|
||||
temp := res
|
||||
for i := 1; i < len(nums); i++ {
|
||||
temp.Next = &ListNode{
|
||||
Val: nums[i],
|
||||
}
|
||||
temp = temp.Next
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
@@ -226,7 +226,7 @@
|
||||
|203|[Remove Linked List Elements](./Algorithms/203.remove-linked-list-elements)|33%|Easy||
|
||||
|204|[Count Primes](./Algorithms/204.count-primes)|26%|Easy|[❤](https://leetcode.com/list/oussv5j)|
|
||||
|205|[Isomorphic Strings](./Algorithms/205.isomorphic-strings)|34%|Easy|[❤](https://leetcode.com/list/oussv5j)|
|
||||
|206|[Reverse Linked List](./Algorithms/206.reverse-linked-list)|46%|Easy||
|
||||
|206|[Reverse Linked List](./Algorithms/206.Reverse-Linked-List)|46%|Easy||
|
||||
|207|[Course Schedule](./Algorithms/207.course-schedule)|33%|Medium|[❤](https://leetcode.com/list/oussv5j)|
|
||||
|208|[Implement Trie (Prefix Tree)](./Algorithms/208.implement-trie-prefix-tree)|30%|Medium|[❤](https://leetcode.com/list/oussv5j)|
|
||||
|209|[Minimum Size Subarray Sum](./Algorithms/209.minimum-size-subarray-sum)|32%|Medium||
|
||||
|
||||
Reference in New Issue
Block a user