题解 | #划分链表#
划分链表
http://www.nowcoder.com/practice/1dc1036be38f45f19000e48abe00b12f
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
#常规的划分成两个子链表,最后两个子链表合并
#
# @param head ListNode类
# @param x int整型
# @return ListNode类
#
class Solution:
def partition(self , head , x ):
# write code here
left=ListNode(0)
left_tail=left
right=ListNode(0)
right_tail=right
while head:
temp=head
head=head.next
temp.next=None
if temp.val<x:
left_tail.next=temp
left_tail=temp
else:
right_tail.next=temp
right_tail=temp
left_tail.next=right.next
return left.next