86 Partition List
来源:互联网 发布:adidas旗舰店淘宝真假 编辑:程序博客网 时间:2024/06/06 09:54
/*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.注意: 当在第一个节点需要调整时,head->val >=val时,需要在head之前插入节点需要一个head_r*//** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* partition(struct ListNode* head, int x){if(!head) return head; struct ListNode *head_r=(struct ListNode*)malloc(sizeof(struct ListNode));head_r->next=head;struct ListNode *insert_r=head_r;struct ListNode *fast=head;while(insert_r->next && insert_r->next->val < x)//寻找第一个不小于x的前一个节点{insert_r=insert_r->next;}struct ListNode *fast_r=insert_r;fast=insert_r->next;//第一个不小于x的节点while(fast){if(fast->val >= x){fast_r=fast;fast=fast->next;}else{fast_r->next=fast->next;fast->next=insert_r->next;insert_r->next=fast;insert_r=fast;fast=fast_r->next;}}return head_r->next;}struct ListNode* partition(struct ListNode* head, int x){if(!head) return head; struct ListNode *head_r=(struct ListNode*)malloc(sizeof(struct ListNode));head_r->next=NULL;struct ListNode *h_r=head_r; struct ListNode *head_b=(struct ListNode*)malloc(sizeof(struct ListNode));head_b->next=NULL;struct ListNode *h_b=head_b;struct ListNode *tmp=head;while(tmp){if(tmp->val < x){h_r->next=tmp;h_r=h_r->next;}else{h_b->next=tmp;h_b=h_b->next;}tmp=tmp->next;}h_b->next=NULL;h_r->next=head_b->next;return head_r->next;}
阅读全文
0 0
- LeetCode 86: Partition List
- LeetCode 86 Partition List
- [leetcode 86] Partition List
- leetcode-86 Partition List
- [LeetCode 86]Partition List
- leetcode || 86、Partition List
- LeetCode---(86) Partition List
- 86 Partition List
- LeetCode 86 Partition List
- Leetcode[86]-Partition List
- 86 Partition List
- leetcode[86]:Partition List
- Leetcode #86 Partition List
- LeetCode(86) Partition List
- leetcode 86: Partition List
- [Leetcode]#86 Partition List
- leetCode #86 Partition List
- leetcode 86:Partition List
- Spire.Email 教程:从C#,VB.NET中的PST文件获取文件夹信息
- String的按值传递,java传参都是传值
- ShaderLab Syntax
- DotNetCore跨平台~在appsettings.json里自定义配置项
- vim 搜索后大小写替换
- 86 Partition List
- linux查找命令总结
- 号小二-您身边的帐号专家!
- CSS网页布局心得(样式优先级,块级元素,内联元素,display,position,float)
- 关于http无状态的思考
- js onclick函数传入参数问题
- 用户权限数据库设计解决方案
- 【威胁通告】Foxit PDF Reader 0day 漏洞
- 02、mysql单表的CRUD