LeetCode:Partition List
来源:互联网 发布:神之浩劫等待游戏数据 编辑:程序博客网 时间:2024/06/04 22:15
重新建立一个链表,遍历原链表,遇到大于等于X的结点放在新链表中,最后将新链表连接到旧链表结尾,这样就不会改变结点的想对位置。
class Solution {public: ListNode *partition(ListNode *head, int x) { if(!head||!head->next) return head; ListNode *l1,*p,*q; l1=NULL;//l1用以保存大于x的序列 //p=l1; //head; while(head&&head->val>=x)//将head循环至从小于x开始,将之前大于x的元素存到l1中 { if(!l1)//不能用p { l1=head; p=l1; } else { p->next=head; p=p->next; } head=head->next; p->next=NULL; } q=head; while(q)//循环剩下的链表 { // q=q->next; if(q->next&&q->next->val>=x) { if(!l1)//不能用p,若用p,则l1始终为空 { l1=q->next; p=l1; } ///head->next=q->next; else { p->next=q->next; p=p->next; } q->next=q->next->next; //p=p->next; p->next=NULL; } if(!q->next)//若q后移为空,则跳出,q处于最后一个结点上 break; if(q->next&&q->next->val<x)//若结点值小于x,则q后移 q=q->next; } if(q) q->next=l1; //q->next=l1; else head=l1; return head; }};
0 0
- 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
- LeetCode - Partition List
- [LeetCode] Partition List
- LeetCode 86: Partition List
- LeetCode:Partition List
- 【leetcode】Partition List
- robotium—只有apk文件的测试
- 在VMware里克隆出来的CentOS,没有eth0的解决办法
- 使用SOS.dll调查内存泄露
- android中SQLite数据库操作例程
- android开发框架AndroidAnnotations介绍
- LeetCode:Partition List
- 微软ERP家具行业解决方案
- POJ 3104 - Drying(二分搜索)
- C++中 explicit的用法
- 二分图判断 (图的搜索)
- android root权限破解分析
- BZOJ 2803 Poi2012 Prefixuffix Hash
- 简单行转列
- HDU2222 Keywords Search