LeetCode OJ:Partition List
来源:互联网 发布:金锐盘开票软件 编辑:程序博客网 时间:2024/05/02 01:11
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
.
算法思想:
两个指针,s将小于x的串起来,e将大于等于x的串起来,最后将s的next指向e,注意处理head指针就好了
/** * 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 NULL; ListNode *p,*q,*e,*s,*start,*end; start=s=new ListNode(0); end=e=new ListNode(0); p=head; while(p){ if(p->val<x){ s->next=p; s=p; } else{ e->next=p; e=p; } p=p->next; } e->next=NULL; s->next=end->next; return start->next; }};
0 0
- LeetCode OJ:Partition List
- LeetCode OJ - Partition List
- LeetCode OJ Partition List
- <LeetCode OJ> 61 / 86 Rotate List / Partition List
- LeetCode OJ 之 Partition List (划分链表)
- LeetCode: Partition List
- LeetCode Partition List
- LeetCode: Partition List
- [Leetcode] Partition List **
- [Leetcode] Partition List
- [LeetCode] Partition List
- leetcode 81: Partition List
- [Leetcode] Partition List
- leetcode 118: Partition List
- [LeetCode]Partition List
- [leetcode]Partition List
- LeetCode-Partition List
- [leetcode] Partition List
- 「索引」《Python基础教程》学习笔记
- (一)简单工厂模式
- 读书笔记:“集体智慧编程”之第三章:带偏好条件的聚类及聚类的展示方式
- 查看表空间占用情况
- unix shell取5个月前的时间
- LeetCode OJ:Partition List
- XBMC皮肤开发入门篇
- R语言学习笔记2——数据集
- <MEMORY>Project Euler NO22
- 自己仿写一个ucosii(四) 任务调度函数
- MPC8572调试记录
- codeforces Multitasking
- Spring 设计模式之单例
- Virtual Judge Contest 1-F