首页 > 代码库 > Partition List

Partition List

 

# Definition for singly-linked list.class ListNode:     def __init__(self, x):         self.val = x         self.next = Noneclass Solution:    # @param head, a ListNode    # @param x, an integer    # @return a ListNode    def partition(self, head, x):        if head==None:            return head
less_node_list=ListNode(0) greater_node_list=ListNode(0) head1=less_node_list head2=greater_node_list
while head!=None: if head.val<x: head1.next=ListNode(head.val) head1=head1.next else: head2.next=ListNode(head.val) head2=head2.next head=head.next
head1.next
=greater_node_list.next return less_node_list.next

 

Partition List