Arena / Problems /

206. Reverse Linked List

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #206

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: [1,2]
Output: [2,1]
Example 3
Input: []
Output: []
Constraints
  • The number of nodes in the list is the range [0, 5000].
  • -5000 <= Node.val <= 5000
Topics & Algorithms
⚔️

Vacant Java Throne

No 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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Java Solution by Anonymous Developer
0
Approach Intuition:
  1. 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.

  2. Which should be the loop condition? while (current != null) or while (current.next != null)?

    Click to view the answer

    It is 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;
    }
}
⚔️

Vacant Python Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
Approach Intuition:
  1. 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.

  2. Which should be the loop condition? while (current != null) or while (current.next != null)?

    Click to view the answer

    It is 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
⚔️

Vacant C++ Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C++ Solution by Anonymous Developer
0
Approach Intuition:
  1. 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.

  2. Which should be the loop condition? while (current != null) or while (current.next != null)?

    Click to view the answer

    It is 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;
    }
};
⚔️

Vacant Javascript Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Javascript Solution by Anonymous Developer
0
Approach Intuition:
  1. 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.

  2. Which should be the loop condition? while (current != null) or while (current.next != null)?

    Click to view the answer

    It is 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
};
⚔️

Vacant C# Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C# Solution by Anonymous Developer
0
Approach Intuition:
  1. 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.

  2. Which should be the loop condition? while (current != null) or while (current.next != null)?

    Click to view the answer

    It is 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;
    }
}
⚔️

Vacant Go Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Go Solution by Anonymous Developer
0
Approach Intuition:
  1. 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.

  2. Which should be the loop condition? while (current != null) or while (current.next != null)?

    Click to view the answer

    It is 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
}
⚔️

Vacant Ruby Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Ruby Solution by Anonymous Developer
0
Approach Intuition:
  1. 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.

  2. Which should be the loop condition? while (current != null) or while (current.next != null)?

    Click to view the answer

    It is 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
⚔️

Vacant Rust Throne

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.

⚔️

Vacant Swift Throne

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.

⚔️

Vacant Kotlin Throne

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.