Tuesday, August 25, 2015

Leetcode 206. Reverse Linked List

https://leetcode.com/problems/reverse-linked-list/

Solution:
# T:O(n) S:O(1)
class Solution:
    # @param {ListNode} head
    # @return {ListNode}
    def reverseList(self, head):
        dummy = ListNode(float("-inf"))
        while head:
            dummy.next, head.next, head = head, dummy.next, head.next
        return dummy.next
Run Time: 60 ms

No comments:

Post a Comment