Reverse Linked List

Easy#206reverse

You get the head of a singly linked list. Turn the list around so that every link points the other way, and return the new head. Our example: 4 → 7 → 2 → 9 — the answer is 9 → 2 → 7 → 4.

input
head: the head of a singly linked list, maybe empty (null).
returns
The head of the list turned around, so that every link points the other way.
constraints
  • 0 ≤ nodes ≤ 500
  • −5000 ≤ Node.val ≤ 5000
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
function reverseList(head) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more linked list pointers problems

Pattern “Linked list pointers” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.