PYTHON 42ms 97th percentile solution O(n) time O(1) space


  • 0
    V
    class Solution(object):
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head:
                return None
    
            prev = head
            probe = head.next
            head.next = None # forgot this one initially
            while probe != None:
                nxt = probe.next 
                probe.next = prev
                prev = probe
                probe = nxt
            
            return prev
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.