Skip to main content

Reverse Linked List

EasyPremium

Given the head of a singly linked list, reverse the list and return its head.

Implement a function reverseList that takes the head of the linked list as input and returns the new head of the reversed list.

Refer to the ListNode class/struct and do not comment it out.

Example

Input: 1 -> 2 -> 3 -> 4 Output: 4 -> 3 -> 2 -> 1

Can you come up with a solution with a time complexity of O(n)?

Can you come up with a solution with a space complexity of O(1)?