leetcode 86. Partition List
来源:互联网 发布:电视盒子软件2017 编辑:程序博客网 时间:2024/05/17 09:25
86. 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
.
way-2: 操作链表
/** * 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) { //way-1 /* vector<int> xiao; vector<int> da; ListNode *p=head; while(p) { if(p->val<x) xiao.push_back(p->val); else da.push_back(p->val); p=p->next; } p=head; for(int i=0;i<xiao.size();i++) { p->val=xiao[i]; p=p->next; } for(int i=0;i<da.size();i++) { p->val=da[i]; p=p->next; } return head; */ //way-2 if (!head) return head; ListNode hhead(-1); hhead.next = head; ListNode * small = &hhead; ListNode * now = head; ListNode * last = small; while (now) { if (now->val >= x) { now = now->next; last = last->next; } else { if (now == small->next) { now = now->next; last = last->next; } else { last->next = now->next; now->next = small->next; small->next = now; now = last->next; } small = small->next; } } return hhead.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
- 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
- LeetCode-86.Partition List
- [Leetcode]86. Partition List
- LeetCode - 86. Partition List
- 超级牛逼的代码注释
- js基本知识点总结-----js函数基础
- dp题目
- Kosaraju算法
- conda更换国内下载镜像
- leetcode 86. Partition List
- 用Python建立最简单的web服务器
- md5 Length Extension Attack
- ssl协议,openssl,创建私有CA
- VS2010中使用sprintf出现warning C4997: 'sprintf': This function or variable may be unsafe.
- 【Scikit-Learn 中文文档】随机梯度下降
- js判断浏览器的关闭标签动作和刷新的事件
- JS使用canvas给图片打马赛克
- 链表知识技巧