链表-leetcode 86 Partition List
来源:互联网 发布:德比鞋 搭配 知乎 编辑:程序博客网 时间:2024/06/06 01:02
原题链接:Partition List
题解:
/** * 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) { /* 思路:类似与快排的partition过程,需要有两个指针分别指向小值和大值的末尾,小于就换位置,大于就下一个 Time Complexity:O(N) Space Complexity:O(1) */ if(!head || !head->next)return head; ListNode* res=new ListNode(0); res->next=head; ListNode* small=res,*max=NULL; bool flag=false; while(head){ if(head->val<x){ if(small->next==head){ small=head; head=head->next; } else{ max->next=head->next; head->next=small->next; small->next=head; small=head; head=max->next; } } else{ if(!flag){ max=head; flag=true; head=head->next; } else{ max=head; head=head->next; } } } return res->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
- 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
- [Leetcode]#86 Partition List
- 最短路径:弗洛伊德算法
- struts2基本配置
- binwalk的extract出来的内容
- JavaWeb(7)——Maven学习
- 具名函数和匿名函数
- 链表-leetcode 86 Partition List
- RedMonk 6 月编程语言排行:JS:第一 Java:第二
- JavaWeb学习总结(十一)——使用Cookie进行会话管理
- 史上最详细Linux 虚拟文件系统sysfs之属性文件attribute 整理(二)
- jQuery根据radio来控制texteara
- 子集
- n&(n-1)的妙用
- JavaWeb学习总结(十二)——Session
- promise