Leetcode - 206. Reverse Linked List
Leetcode - 206. Reverse Linked List
- Given the head of a singly linked list, reverse the list, and return the reversed list.
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseList(head *ListNode) *ListNode {
current := head
var prev, nextFlag *ListNode
for current!=nil{
nextFlag = current.Next
current.Next = prev
prev = current
current = nextFlag
}
return prev
}
This post is licensed under CC BY 4.0 by the author.