Given the head of a singly linked list, reverse the list, and return the reversed list.
[0, 5000].-5000 <= Node.val <= 5000No developer has claimed the Java throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
To solve this problem, we only need to define two variables: current and previous. 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) or while (current.next != null)?
It is Click to view the answer
while (current != null), because the operation to be performed is current.next = previous.
/**
* 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;
}
}
No developer has claimed the Python throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
To solve this problem, we only need to define two variables: current and previous. 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) or while (current.next != null)?
It is Click to view the answer
while (current != null), because the operation to be performed is current.next = 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
No developer has claimed the C++ throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
To solve this problem, we only need to define two variables: current and previous. 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) or while (current.next != null)?
It is Click to view the answer
while (current != null), because the operation to be performed is current.next = 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;
}
};
No developer has claimed the Javascript throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
To solve this problem, we only need to define two variables: current and previous. 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) or while (current.next != null)?
It is Click to view the answer
while (current != null), because the operation to be performed is current.next = 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
};
No developer has claimed the C# throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
To solve this problem, we only need to define two variables: current and previous. 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) or while (current.next != null)?
It is Click to view the answer
while (current != null), because the operation to be performed is current.next = 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;
}
}
No developer has claimed the Go throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
To solve this problem, we only need to define two variables: current and previous. 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) or while (current.next != null)?
It is Click to view the answer
while (current != null), because the operation to be performed is current.next = 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
}
No developer has claimed the Ruby throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
To solve this problem, we only need to define two variables: current and previous. 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) or while (current.next != null)?
It is Click to view the answer
while (current != null), because the operation to be performed is current.next = 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
No developer has claimed the Rust throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #206 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.