Three concise python solution.

  • 7

    Method 1: uses self as a dummy node for swapping

    def reverseList(self, head):
        pre,, tail = self, head, head
        while tail and
  ,, = \

    Method 2: swapping using two variable, pre and cur

    def reverseList(self, head):
        pre, cur = None, head
        while cur:
  , pre, cur = pre, cur,
        return pre

    Method 3: Recursive method by storing previous node in the function argument.

    def reverseList(self, head, pre = None):
        if not head: return pre
        cur, =, pre
        return self.reverseList(cur, head)

  • 0

    Amazing solution. Very concise. Thanks for sharing!

Log in to reply

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