# 分隔链表

<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ，请你对链表进行分隔，使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p><p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0086.Partition%20List/images/partition.jpg" style="width: 662px; height: 222px;" /><pre><strong>输入：</strong>head = [1,4,3,2,5,2], x = 3<strong><br />输出</strong>：[1,2,2,4,3,5]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>head = [2,1], x = 2<strong><br />输出</strong>：[1,2]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>链表中节点的数目在范围 <code>[0, 200]</code> 内</li>	<li><code>-100 <= Node.val <= 100</code></li>	<li><code>-200 <= x <= 200</code></li></ul>

## template

```python
class ListNode(object):
	def __init__(self, x):
		self.val = x
		self.next = None
class LinkList:
	def __init__(self):
		self.head=None
	def initList(self, data):
		self.head = ListNode(data[0])
		r=self.head
		p = self.head
		for i in data[1:]:
			node = ListNode(i)
			p.next = node
			p = p.next
		return r
	def	convert_list(self,head):
		ret = []
		if head == None:
			return
		node = head
		while node != None:
			ret.append(node.val)
			node = node.next
		return ret
class Solution(object):
	def partition(self, head, x):
		"""
		:type head: ListNode
		:type x: int
		:rtype: ListNode
		"""
		if head is None:
			return None
		less = lesshead = None
		last = pos = head
		while pos is not None:
			if pos.val < x:
				if lesshead is None:
					lesshead = pos
				else:
					less.next = pos
				less = pos
				if head == pos:
					last = head = pos.next
				else:
					last.next = pos.next
			else:
				last = pos
			pos = pos.next
		if lesshead is not None:
			less.next = head
		else:
			lesshead = head
		return lesshead
# %%
l = LinkList()
list1 = [1,4,3,2,5,2]
l1 = l.initList(list1)
x = 3
s = Solution()
print(l.convert_list(s.partition(l1, x)))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```