Follow

Follow

# LeetCode Solution, Easy, 206. Reverse Linked List

·Jun 27, 2022·

## 題目敘述

Given the `head` of a singly linked list, reverse the list, and return the reversed list.

Example 1:

``````Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
``````

Example 2:

``````Input: head = [1,2]
Output: [2,1]
``````

Example 3:

``````Input: head = []
Output: []
``````

Constraints:

• The number of nodes in the list is the range `[0, 5000]`.
• `-5000 <= Node.val <= 5000`

Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

## 解法解析

### 解法範例

#### Go

##### Iterative
``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
var prev *ListNode
for cur != nil {
next := cur.Next
cur.Next = prev
prev = cur
cur = next
}

return prev
}
``````
##### Recursive
``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
}

return p
}
``````

#### JavaScript

##### Iterative
``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
var reverseList = function (head) {
let [prev, current] = [null, head];
while (current) {
[current.next, prev, current] = [prev, current, current.next];
}
return prev;
};
``````
##### Recursive
``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
var reverseList = function (head) {
}
return p;
};
``````

#### Kotlin

##### Iterative
``````/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* class ListNode(var `val`: Int) {
*     var next: ListNode? = null
* }
*/
class Solution {
var prev: ListNode? = null

while (current != null) {
val next = current.next
current.next = prev
prev = current
current = next
}

return prev
}
}
``````
##### Recursive
``````/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* class ListNode(var `val`: Int) {
*     var next: ListNode? = null
* }
*/
class Solution {

return p
}
}
``````

#### PHP

##### Iterative
``````/**
* Definition for a singly-linked list.
* class ListNode {
*     public \$val = 0;
*     public \$next = null;
*     function __construct(\$val = 0, \$next = null) {
*         \$this->val = \$val;
*         \$this->next = \$next;
*     }
* }
*/
class Solution
{

/**
* @return ListNode
*/
{
\$prev = null;

while (!is_null(\$current)) {
\$next = \$current->next;
\$current->next = \$prev;
\$prev = \$current;
\$current = \$next;
}

return \$prev;
}
}
``````
##### Recursive
``````/**
* Definition for a singly-linked list.
* class ListNode {
*     public \$val = 0;
*     public \$next = null;
*     function __construct(\$val = 0, \$next = null) {
*         \$this->val = \$val;
*         \$this->next = \$next;
*     }
* }
*/
class Solution
{

/**
* @return ListNode
*/
{
}

return \$p;
}
}
``````

#### Python

##### Iterative
``````# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def reverseList(self, head: ListNode | None) -> ListNode | None:
prev = None
while curr:
next_temp = curr.next
curr.next = prev
prev = curr
curr = next_temp

return prev
``````
##### Recursive
``````# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def reverseList(self, head: ListNode | None) -> ListNode | None:

return p
``````

#### Rust

``````
``````

#### Swift

##### Iterative
``````/**
* public class ListNode {
*     public var val: Int
*     public var next: ListNode?
*     public init() { self.val = 0; self.next = nil; }
*     public init(_ val: Int) { self.val = val; self.next = nil; }
*     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func reverseList(_ head: ListNode?) -> ListNode? {
var prev: ListNode? = nil
while curr != nil {
let next = curr?.next
curr?.next = prev
prev = curr
curr = next
}
return prev
}
}
``````
##### Recursive
``````/**
* public class ListNode {
*     public var val: Int
*     public var next: ListNode?
*     public init() { self.val = 0; self.next = nil; }
*     public init(_ val: Int) { self.val = val; self.next = nil; }
*     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func reverseList(_ head: ListNode?) -> ListNode? {
}