Partition List
来源:互联网 发布:北京新机场 知乎 编辑:程序博客网 时间:2024/05/18 14:23
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.
/** * 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) { if(!head){ return head; } ListNode leftDummy(0); leftDummy.next = NULL; ListNode rightDummy(0); rightDummy.next = NULL; ListNode * curLeft = &leftDummy; ListNode * curRight = &rightDummy; ListNode * cur = head; while(cur){ if(cur->val < x){ curLeft->next = cur; curLeft = curLeft->next; }else{ curRight->next = cur; curRight = curRight->next; } cur = cur->next; } curLeft->next = NULL; curRight->next = NULL; if(!leftDummy.next){ return rightDummy.next; }else{ curLeft->next = rightDummy.next; return leftDummy.next; } }};
0 0
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- 1年日记(5月篇)
- 学习Unity射线的创建使用
- 样式和主题
- java并发编程(14)--java的守护线程与非守护线程
- HDU 1544Palindromes(求回文字符串)
- Partition List
- RS WLAN产品测试全面解决方案
- 自由落体
- vtk实战(五十)—计算线段与多边形的交点
- UVA 11636 Hello World!
- C语言位运算
- 通信协议:HTTP、TCP、UDP
- Centos MAC远程连接服务器
- 自动创建设备文件