Reverse Linked List

 

206. Reverse Linked List

Solutions

  1. 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
    
  2. 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)