Partition List

 

# Definition for singly-linked list.
class ListNode:
     def __init__(self, x):
         self.val = x
         self.next = None

class 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

 

posted @ 2014-10-10 17:45  clq.lib  阅读(123)  评论(0编辑  收藏  举报