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.

class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if(!head || !(head->next) ) return head;
ListNode *current = head;
ListNode *smallPointer = NULL; //point to the last node <x
ListNode *largePointer = NULL; //point to the last node >x
while(current)
{
if(current->val >= x)
{
largePointer = current;
current = current->next;
}
else
{
if(!largePointer)
{
smallPointer = current;
current = current->next;
}
else if(smallPointer)
{
largePointer->next = smallPointer->next;
smallPointer -> next = current;
current = current->next;
smallPointer = smallPointer->next;
smallPointer->next = largePointer->next;
largePointer->next = current;
}
else //head
{
smallPointer = current;
current = current->next;
smallPointer->next = head;
head = smallPointer;
largePointer->next = current;
}
}
}
return head; }
};

86. Partition List (List)的更多相关文章

  1. leetcode 143. Reorder List 、86. Partition List

    143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...

  2. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  3. 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 ...

  4. 86. Partition List

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

  5. [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 ...

  6. LeetCode OJ 86. Partition List

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

  7. 【一天一道LeetCode】#86. Partition List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. LeetCode 86. Partition List 划分链表 C++

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

  9. [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 ...

  10. 【Leetcode】86. Partition List

    Question: Given a linked list and a value x, partition it such that all nodes less than x come befor ...

随机推荐

  1. 笔记:使用 stylus stylus-loader

    笔记:使用 stylus stylus-loader 安装 stylus Stylus-loader cnpm i stylus stylus-loader --save 配置 webpack.con ...

  2. callback&&callback()

    如果存在回调函数就执行!这是利用了 JS &&符号的一个小技巧&& 符号在前面为假时就不会执行后面的语句了所以这个就相当于 if(callback){ callback ...

  3. pandas之DateFrame 数据过滤+遍历行+读写csv-txt-excel

    # XLS转CSV df = pd.read_excel(r'列表.xls') df2 = pd.DataFrame()df2 = df2.append(list(df['列名']), ignore_ ...

  4. awk&sed 小实例

    1.打印文件奇数行sed -n 'p;n'sed 'n;d' sed -n '$!N;P'sed -n '1~2p'awk 'i=!i'awk 'NR%2'2.打印文件偶数行sed -n 'n;p's ...

  5. laravel加载js和css等资源

    4里面是composer下载以后,publish,blade模板里面有html标签 不过在5以后,html和form标签去掉了,publish方式似乎也变化了,没看懂…… 直接贴demo吧 mac:n ...

  6. golang的指针到string,string到指针的转换

    由于某个需求,需要如题的转换,废话不多说,直接贴代码了,其实挺丑了,备用了 func (this *Server) socketParserHandler(client *genTcpServer.C ...

  7. FU-A 分包

    FU-A分包方式,以及从RTP包里面得到H.264数据和AAC数据的方法 [原创] RFC3984是H.264的baseline码流在RTP方式下传输的规范,这里只讨论FU-A分包方式,以及从RTP包 ...

  8. python学习(二十三) String(下) 分片和索引

    分片: 记住, 是开闭区间. a = "abcdef"print(a[:])print(a[1:])print(a[:3])print(a[-2])print(a[:-2])pri ...

  9. Linux Platform devices 平台设备驱动

    设备总线驱动模型:http://blog.csdn.net/lizuobin2/article/details/51570196 本文主要参考:http://www.wowotech.net/devi ...

  10. qcow2磁盘加密及libvirt访问

      1.创建qcow2加密磁盘[root@Coc-5 test_encrypt]# qemu-img convert -f qcow2 -O qcow2 -o encryption template_ ...