Visit original link: 206. Reverse Linked List - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions for a better experience!
LeetCode link: 206. Reverse Linked List, difficulty: Easy.
Given the head of a singly linked list, reverse the list, and return the reversed list.
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Input: [1,2]
Output: [2,1]
Input: []
Output: []
- The number of nodes in the list is the range
[0, 5000]. -5000 <= Node.val <= 5000
-
To solve this problem, we only need to define two variables:
currentandprevious. How do we inverse two node?Click to view the answer
`current.next = previous` is the inversion.
-
Which should be the loop condition?
while (current != null)orwhile (current.next != null)?Click to view the answer
It is `while (current != null)`, because the operation to be performed is `current.next = previous`.
-
Traverse all nodes.
previous = null current = head while (current != null) { current = current.next }
-
Add
current.next = previous.previous = null current = head while (current != null) { tempNext = current.next current.next = previous current = tempNext }
-
Currently
previousis alwaysnull, we need to change it:previous = current.previous = null current = head while (current != null) { tempNext = current.next current.next = previous previous = current current = tempNext }
- Time complexity:
O(N). - Space complexity:
O(1).
/**
* 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;
}
}# 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/**
* 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;
}
};/**
* 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
};/**
* 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;
}
}/**
* 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
}# 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// Welcome to create a PR to complete the code of this language, thanks!Dear LeetCoders! For a better LeetCode problem-solving experience, please visit website LeetCode.to: Dare to claim the best practices of LeetCode solutions! Will save you a lot of time!
Original link: 206. Reverse Linked List - LeetCode Python/Java/C++/JS/C#/Go/Ruby Solutions.
GitHub repository: leetcode-python-java.

