Reverse linked list

July 15, 2022

linked-list

Problem URL: Reverse linked list

We start with a null node called previous, iterate through the list, and move it's next printer previous on and then repeat it till the end. Then we just return the previous which is not the current head of the list.

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        prev = None
        current = head
        while current:
            next = current.next
            current.next = prev
            prev = current
            current = next
        return prev

Time Complexity: O(n)
Space Complexity: O(1)