Skip to content

206. Reverse Linked List

206. Reverse Linked List

Companies: Amazon, Apple, Bloomberg, Facebook, Goldman Sachs, Google, Microsoft, Oracle, Uber
Date: September 5, 2021
Difficulty: Easy
Review Date: October 3, 2021
Status: Done
Tags: Linked 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]:
        if not head or not head.next:
            return head

        prev = current = head
        nextNode = head.next
        while current and nextNode:
            current = nextNode
            nextNode = current.next
            current.next = prev
            prev = current

        head.next = None

        return prev

Last update : 25 mai 2024
Created : 25 mai 2024