easyLinked ListRecursion 0 views

Reverse a Linked List

Reverse a singly-linked list in place and return the reversed list.

Given the head of a singly-linked list, reverse the list and return the head of the reversed list.

Each node's next pointer should end up pointing at the node that originally came before it, so the list ends up traversed in the opposite order.

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: []

Explanation: An empty list reverses to itself.

Constraints

  • The number of nodes in the list is in the range [0, 5000].
  • -5000 <= Node.val <= 5000

Follow-up

Can you implement this both iteratively and recursively?

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.