Friday, September 12, 2014

Leetcode: Partition List @Python

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param head, a ListNode
    # @param x, an integer
    # @return a ListNode
    def partition(self, head, x):
        if not head: return head
        nless=less=ListNode(0)
        ngeq=geq=ListNode(0)
        pre,crt=None,head
        while crt:
            pre=crt
            crt=crt.next
            if pre.val<x:
                less.next=pre
                less=pre
                less.next=None
            else:
                geq.next=pre
                geq=pre
                geq.next=None
        less.next=ngeq.next
        return nless.next

No comments :

Post a Comment