Beats 85% of python submissions.


  • 0
    S
    # Def
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head == None or head.next == None:
                return head
            else:
                cur = head
                while head.next:
                    nex = head.next
                    #give current head pointer & next pointer
                    head.next = nex.next
                    nex.next = cur
                    #reverse
                    cur = nex
                    #update the current head
                return cur
    

    I reverse the list one by one,kind of silly :P


Log in to reply
 

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