#Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = NoneclassSolution(object):defpartition(self,head,x):""" :type head: ListNode :type x: int :rtype: ListNode """ifnothead:returnNoneright=ListNode(-1)newhead=ListNode(-1)left_head=newheadright_head=right# assign the nodes into two listswhilehead!=None:newNode=ListNode(head.val)ifhead.val<x:left_head.next=newNodeleft_head=left_head.nextifhead.val>=x:right_head.next=newNoderight_head=right_head.nexthead=head.next# connect two listsleft_head.next=right.nextreturnnewhead.next