# LeetCode 86 | 链表基础，一次遍历处理链表中所有符合条件的元素

## 题意

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5


## 题解

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
# 创建两个链表
left = ListNode(0)
right = ListNode(0)
# 以及用来遍历这两个链表的指针
ln = left
rn = right
while pnt is not None:
# 小于x则插入left，否则插入right
if pnt.val < x:
ln.next = ListNode(pnt.val)
ln = ln.next
else:
rn.next = ListNode(pnt.val)
rn = rn.next
pnt = pnt.next

# 将left与right合并
ln.next = right.next
return left.next


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
return None

# 找到结尾，当找到了大于等于x的元素就放入结尾后面
while tail.next is not None:
tail = tail.next

# 记录遍历的终点
end_point = None

pnt = ListNode(0)

while pnt.next is not end_point:
cur = pnt.next
if cur.val >= x:
# 插入在末尾
tail.next = cur
tail = cur
# 如果终点是None的话则进行更新
# end_point只会更新一次
if end_point is None:
end_point = cur
pnt.next = cur.next
continue
pnt = pnt.next
tail.next = None