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
本文标题:86. Partition List
本文链接:https://www.haomeiwen.com/subject/ihblvttx.html
网友评论