FastPrepReverse a Linked List

Reverse a Linked List

Apple logoApple● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given the head of an acyclic singly linked list, reverse all of its next pointers and return the new head. Reuse the original data nodes. An empty list returns null.

Input and output lists are displayed as their node-value sequences. For this exercise, the report's brief reverse-list recollection is practiced as reversal of a singly linked list.

Function

reverseList(head: ListNode) → ListNode

Examples

Example 1

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

The original final node becomes the new head.

Example 2

head = []return = []

Reversing an empty list still produces null.

Constraints

  • The list contains between 0 and 10000 nodes.
  • -10^9 <= node.val <= 10^9.

More Apple problems

See Apple hiring insights
public ListNode reverseList(ListNode head) {
    // Write your solution here.
}
head[1,2,3,4]
expected[4,3,2,1]
Checking account…