86. Partition List

# 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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容