Simple python iterative solution


  • 1
    R

    Simple python solution with 3 ListNodes.

    def reverseList(self, head):
            first = head
            current = None
            previous = None
            while first:
                current = first
                first = first.next
                current.next = previous
                previous = current
            return current

  • 0
    S

    You can do it shorter in Python:

    def reverseList(self, head):
        prev = None
        while head:
            head.next, prev, head = prev, head, head.next
        return prev

Log in to reply
 

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