"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of linked list.
@param x: an integer
@return: a ListNode
"""
def partition(self, head, x):
# write your code here
if head == None or head.next == None:
return head
node = ListNode(-1000)
node.next = head
head = node
p1 = p2 = head
while p1 != None and p1.val < x:
p1 = p1.next
if p1 == None:
return head.next
while p2.next != p1:
p2 = p2.next
while p1.next != None:
if p1.next.val < x:
p3 = p1.next
p1.next = p1.next.next
p3.next = p2.next
p2.next = p3
p2 = p2.next
continue
p1 = p1.next
return head.next
LintCode:链表划分
最新推荐文章于 2020-02-27 08:30:24 发布