Reverse Linked List
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
- 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
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more linked list pointers problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.