Skip to main content

206 - Reverse Linked List

Details

KeyValue
Linkhttps://leetcode.com/problems/reverse-linked-list/
LanguagePython 3
Runtime54 ms, faster than 48.90% of Python3 online submissions for Reverse Linked List
Memory Usage15.4 MB, less than 55.56% of Python3 online submissions for Reverse Linked List
DatastructuresListNode (linked list)
AlgorithmsIterative
ComplexityTime: O(n) Space: O(1)

Procedure

  1. ...

Code

# 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]:
last = None
while head:
last, head.next, head = head, last, head.next

return last