博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
86. Partition List (List)
阅读量:5007 次
发布时间:2019-06-12

本文共 1661 字,大约阅读时间需要 5 分钟。

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 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; }};

 

转载于:https://www.cnblogs.com/qionglouyuyu/p/4853113.html

你可能感兴趣的文章
UIScrollView 原理
查看>>
linux在tomcat中指定jdk
查看>>
vue学习(二)Vue常用指令
查看>>
《中国作者英文科学写作中的常见语法问题(一)》
查看>>
如何撰写SCI论文的讨论部分?——经典结构 – 俗称“倒漏斗型。
查看>>
织梦dedeCMS数据库结构字段说明-简略说明
查看>>
Cocoa touch(二):UIApplication
查看>>
【数据结构与算法C】利用两个栈S1S2模拟一个队列,用栈的基本操作实线EnQueue,DeQueue,QueueEmpty ...
查看>>
Python-编码和二进制-运算符
查看>>
BZOJ-3211花神游历各国 并查集+树状数组
查看>>
【BZOJ-3667】Rabin_Miller算法 随机化判素数
查看>>
KVC与KVO
查看>>
[HDU5015]233 Matrix
查看>>
慢连接
查看>>
.NET 平台技术官网链接
查看>>
【计算机视觉】SIFT中LoG和DoG比较
查看>>
数学计算公式
查看>>
链表小结
查看>>
【腾讯Bugly干货分享】手游热更新方案xLua开源:Unity3D下Lua编程解决方案
查看>>
php-fpm配置
查看>>