反转链表:头插法,保持原次序,尾插法
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
遍历,如果小于跳过,如果大于等于,尾插入,合并链表
"""
another_head=another=ListNode()
ans=cur=ListNode()
cur.next=head
while cur.next:
if cur.next.val>=x:
temp =cur.next
cur.next=cur.next.next
another.next=temp
another=another.next
another.next=None
else:
cur=cur.next
cur.next=another_head.next
return ans.next