CodePark/ problems / Reverse a Linked List
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Linked lists

Reverse a Linked List

easyabout 12 minutes in an interview

Reverse a singly linked list and return its new head.

A singly linked list is a chain of nodes. Each ListNode holds a val and a next reference to the following node. The last node's next is null.

You get the head of the list. The head is the first node. Reuse the existing nodes by changing their next references. Don't build new nodes.

  • head can be null (an empty list). In that case, return null.
  • The list can hold up to 100,000 nodes.
Input: head = 1 → 2 → 3 → 4 Output: 4 → 3 → 2 → 1
Input: head = 7 Output: 7
Input: head = null Output: null