Solutions
- Solution 1: Iterative
def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head == None: return head next = head.next last = None while head!=None: tmp = head.next head.next = last last = head head = tmp return last
- Solution 2: Recursive
def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ def reverse(prev, curr): if curr == None: return prev else: tmp = curr.next curr.next = prev return reverse(curr, tmp) return reverse(None, head)
PREVIOUSImage Alignment