← Practice · Linked list

Reverse Linked List Easy

Reverse a singly linked list and return the new head. The input is given as a ListNode (fields val, next), or null for an empty list.

Examples

  • 1->2->3->4->5 becomes 5->4->3->2->1
  • 1->2 becomes 2->1

Define a function named reverseList in JavaScript. ListNode (val, next) is available.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests