Ugly Python solution harnessing node.val


  • 0
    W
    class Solution(object):
        def reverseList(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if not head:
                return None
            else:
                new = ListNode(-1)
                new.next, curr, vals = head, new, []
                while curr.next is not None:
                    vals.append (curr.next.val)
                    curr = curr.next
                vals, i, curr = vals[::-1], 0, new
                while curr.next is not None:
                    curr.next.val, curr, i = vals[i], curr.next, i+1
                return new.next
    

Log in to reply
 

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