力扣链接:206. 反转链表,难度:简单。
给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
示例 1:

输入: head = [1,2,3,4,5]
输出: [5,4,3,2,1]
示例 2:

输入: [1,2]
输出: [2,1]
示例 3:
输入: []
输出: []
约束:
- 链表中节点的数目范围是
[0, 5000]
-5000 <= Node.val <= 5000
思路
解决这个问题,只需要定义两个变量:
current
和previous
。点击查看答案
current.next = previous
就是反转了。循环条件是
while (current != null)
,还是while (current.next != null)
?点击查看答案
是
while (current != null)
,因为需要操作的是current.next = previous
。
步骤
遍历所有节点。
previous = null current = head while (current != null) { current = current.next }
加入
current.next = previous
。previous = null current = head while (current != null) { tempNext = current.next current.next = previous current = tempNext }
previous
目前始终是null
,需要让它变化起来:previous = current
。previous = null current = head while (current != null) { tempNext = current.next current.next = previous previous = current current = tempNext }
复杂度
时间复杂度
O(N)
空间复杂度
O(1)
Java #
/**
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode previous = null;
var current = head;
while (current != null) {
var tempNext = current.next;
current.next = previous;
previous = current;
current = tempNext;
}
return previous;
}
}
Python #
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
previous = None
current = head
while current:
temp_next = current.next
current.next = previous
previous = current
current = temp_next
return previous
C++ #
/**
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* previous = nullptr;
ListNode* current = head;
while (current) {
auto temp_next = current->next;
current->next = previous;
previous = current;
current = temp_next;
}
return previous;
}
};
JavaScript #
/**
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
var reverseList = function (head) {
let previous = null
let current = head
while (current != null) {
const tempNext = current.next
current.next = previous
previous = current
current = tempNext
}
return previous
};
C# #
/**
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution
{
public ListNode ReverseList(ListNode head)
{
ListNode previous = null;
ListNode current = head;
while (current != null)
{
var tempNext = current.next;
current.next = previous;
previous = current;
current = tempNext;
}
return previous;
}
}
Go #
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseList(head *ListNode) *ListNode {
var previous *ListNode
current := head
for current != nil {
tempNext := current.Next
current.Next = previous
previous = current
current = tempNext
}
return previous
}
Ruby #
# class ListNode
# attr_accessor :val, :next
#
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
def reverse_list(head)
previous = nil
current = head
while current
temp_next = current.next
current.next = previous
previous = current
current = temp_next
end
previous
end