Algorithm


Problem Name: 206. Reverse Linked List

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

Code Examples

#1 Code Example with C Programming

Code - C Programming


struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode *a, *b, *c;
    
    a = NULL;
    b = head;
    while (b) {
        c = b->next;
        b->next = a;
        a = b;
        b = c;
    }
​
    return a;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
head = [1,2,3,4,5]

Output

x
+
cmd
[5,4,3,2,1]

#2 Code Example with C++ Programming

Code - C++ Programming


// Iterative
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* pre(NULL), *cur(head), *next;
        while(cur){
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
};

// Recursive
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if (!head || !head->next) return head;
        ListNode* node = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return node;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
head = [1,2,3,4,5]

Output

x
+
cmd
[5,4,3,2,1]

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  public ListNode reverseList(ListNode head) {
    ListNode prev = null;
    ListNode next = null;
    ListNode curr = head;
    while (curr != null) {
      next = curr.next;
      curr.next = prev;
      prev = curr;
      curr = next;
    }
    return prev;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
head = [1,2]

Output

x
+
cmd
[2,1]

#4 Code Example with Javascript Programming

Code - Javascript Programming


const reverseList = function(head) {
  if(head == null) return head
  const pre = new ListNode(null, head)
  let cur = head
  while(cur.next) {
    let tmp = pre.next
    pre.next = cur.next
    cur.next = cur.next.next
    pre.next.next = tmp
  }

  return pre.next
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
head = [1,2]

Output

x
+
cmd
[2,1]

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def reverseList(self, head: ListNode, pre = None) -> ListNode:
        if head:
            nex = head.next
            head.next = pre
            return self.reverseList(nex, head) if nex else head
Copy The Code & Try With Live Editor

Input

x
+
cmd
head = []

Output

x
+
cmd
[]

#6 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0206_ReverseLinkedList
    {
        public ListNode ReverseList(ListNode head)
        {
            var dummyHead = new ListNode(-1);

            var current = head;
            while (current != null)
            {
                var next = current.next;

                current.next = dummyHead.next;
                dummyHead.next = current;

                current = next;
            }

            return dummyHead.next;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
head = []

Output

x
+
cmd
[]
Advertisements

Demonstration


Previous
#205 Leetcode Isomorphic Strings Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#207 Leetcode Course Schedule Solution in C, C++, Java, JavaScript, Python, C# Leetcode