mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-30 23:52:03 +08:00
47 lines
617 B
Markdown
47 lines
617 B
Markdown
# [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/)
|
|
|
|
## 题目
|
|
|
|
Reverse a singly linked list.
|
|
|
|
## 题目大意
|
|
|
|
翻转单链表
|
|
|
|
|
|
## 解题思路
|
|
|
|
按照题意做即可。
|
|
|
|
## 代码
|
|
|
|
```go
|
|
|
|
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
|
|
}
|
|
|
|
``` |