# [leetcode] 【链表】86. Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,

return 1->2->2->4->3->5.

# 题意

Given 1->4->3->2->5->2 and x = 3,

return 1->2->2->4->3->5.

# 题解

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode small(-1);
ListNode big(-1);
auto small_p=&small;
auto big_p=&big;
for(ListNode *cur=head;cur;cur=cur->next)
{
if(cur->val<x)
{
small_p->next=cur;
small_p=cur;
}
else
{
big_p->next=cur;
big_p=cur;
}
}
big_p->next=NULL;
small_p->next=big.next;
return small.next;
}
};

• 本文已收录于以下专栏：

## LeetCode 86 Partition List

LeetCode 86 Partition List

## [LeetCode]86.Partition List

【题目】 Given a linked list and a value x, partition it such that all nodes less than x come before n...

## leetcode[86]:Partition List

Partition ListGiven a linked list and a value x, partition it such that all nodes less than x come b...

## Leetcode 86. Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr...

## leetcode || 86、Partition List

problem： Given a linked list and a value x, partition it such that all nodes less than x come ...

## Leetcode 86. Partition List(链表划分)

Partition List（链表划分）

## LeetCode-86. Partition List (JAVA)分区链表

LeetCode-86. Partition List (JAVA)分区链表

## [LeetCode-86] Partition List （链表数据分区）

Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)