# 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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- Given a linked list and a value x, partition it such that...
- Description Given a linked list and a value x, partition ...
- 原题 给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。 你应该保留两部分内链表节点原...