FastPrepReverse a Singly Linked List

Reverse a Singly Linked List

Chubb logoChubb● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given the head of a finite, acyclic singly linked list, reverse every next pointer and return the new head.

Reuse the original nodes and preserve every value. Return null for an empty list.

Function

reverseList(head: ListNode) → ListNode

Examples

Example 1

head = [1,2,3,4]return = [4,3,2,1]

The original tail becomes the new head and every link points backward.

Example 2

head = []return = []

An empty list remains empty.

Constraints

  • 0 <= number of nodes <= 100000.
  • -1000000000 <= node.val <= 1000000000.
  • The input list is finite and acyclic.

More Chubb problems

See Chubb hiring insights
/**
 * Definition for singly-linked list.
 * class ListNode {
 *   int val;
 *   ListNode next;
 * }
 */
public ListNode reverseList(ListNode head) {
    // Write your solution here.
}
head[1,2,3,4]
expected[4,3,2,1]
Checking account…