ntree.ai
ntree.aiReverse Linked List

Reverse Linked List

easysolvedattempted

Given the head of a singly linked list, return the head of the reversed list.

Example 1:

1 → 2 → 3 → 4
Input:  head = [1, 2, 3, 4]
Output: [4, 3, 2, 1]

Example 2:

1 → 2
Input:  head = [1, 2]
Output: [2, 1]

Example 3:

Input:  head = []
Output: []

Constraints:

  • 0 <= the number of nodes in the list <= 5000
  • -5000 <= Node.val <= 5000
# Definitions:
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
 
def reverseList(head: ListNode) -> ListNode:
pass
 
1→2→3→4

Press Visualise to step through your code.