FastPrepReverse Linked List

Reverse Linked List

The Walt Disney Company logoThe Walt Disney Company● EasyFULLTIMEOAONSITE INTERVIEW

Problem statement

Given the head of a singly linked list, reverse its node order by changing its next pointers and return the new head. Keep every node value unchanged.

The input is a finite acyclic list. Its displayed array lists node values from head to tail; [] represents a null head.

Function

reverseList(head: ListNode) → ListNode

Examples

Example 1

head = [4,1,7]return = [7,1,4]

The node order 4 → 1 → 7 becomes 7 → 1 → 4. The new tail points to null, so the result is [7,1,4].

Example 2

head = []return = []

There are no nodes to reverse. Return null, displayed as [].

Constraints

  • 0 <= head.length <= 500
  • -1000000 <= head[i] <= 1000000

More The Walt Disney Company problems

See The Walt Disney Company hiring insights
Saved
public ListNode reverseList(ListNode head) {
    // Write your code here
}
Checking account…