Dodomeki
Sign in

2. Reverse Linked List

Easy

Given the first node head of a linked list, reverse the list and return the new first node.

Each node is a ListNode with fields val and next.

Example 1
Input: head = [1, 2, 3, 4]
Output: [4, 3, 2, 1]
Example 2
Input: head = []
Output: []
Constraints
  • 0 <= tamanho da lista <= 5000
  • -5000 <= val <= 5000
Linked listRecursion
Caso 1
head =
[1,2,3,4]
Expected =
[4,3,2,1]
Caso 2
head =
[]
Expected =
[]

Run checks these 2 cases. Submit runs all 5, including hidden ones.