美文网首页
86. Partition List

86. Partition List

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-24 12:25 被阅读0次
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def partition(self, head, x):
        """
        :type head: ListNode
        :type x: int
        :rtype: ListNode
        """
        #dummy node to keep track of the head and tail of the smaller list and larger list 
        h1=t1=ListNode(0)
        h2=t2=ListNode(0)
        while head:
            if head.val<x:
                t1.next=head
                t1=t1.next
            else:
                t2.next=head
                t2=t2.next
            head=head.next
        t1.next=h2.next
        t2.next=None
        return h1.next

相关文章

网友评论

      本文标题:86. Partition List

      本文链接:https://www.haomeiwen.com/subject/ihblvttx.html