LeetCode之Partition List
来源:互联网 发布:吉利电动车知豆d2 编辑:程序博客网 时间:2024/04/27 22:18
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /*本题考查链表的遍历和尾插法(不改变相对顺序),具体方法如下: 从头开始遍历,找到小于x的结点,用尾插法加入即可。*/class Solution {public: ListNode *partition(ListNode *head, int x) { if(!head) return head; ListNode newHead(-1); ListNode *mark = &newHead;//小于x的结点插到mark结点后面 mark->next = head; while(mark->next && mark->next->val < x) mark = mark->next; ListNode *pre = mark; if(!pre->next) return head; ListNode *cur = pre->next; while(cur){ if(cur->val < x){ ListNode *tmp = cur; pre->next = cur->next; cur = cur->next; tmp->next = mark->next; mark->next = tmp; mark = mark->next; } else{ pre = pre->next; cur = cur->next; } } return newHead.next; }};
0 0
- LeetCode 之 Partition List
- LeetCode之Partition List
- leetcode之Partition List
- LeetCode之Partition List
- leetcode之Partition List
- leetcode之Partition List
- Leetcode之partition-list
- leetcode:单链表之Partition List
- Leetcode之Partition List 问题
- LeetCode 之 Partition List — C++ 实现
- 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
- 进程的三大状态以及转换
- mongodb查询速度慢是什么原因?
- copy和mutablecopy
- FMS3系列(三):创建基于FMS的流媒体播放程序,看山寨帮的山寨传奇
- 递归实现十进制转二进制
- LeetCode之Partition List
- 图片
- 懒加载
- James的安装与使用
- 借助 exec 函数将字符串转成SQL Server命令执行
- 屌丝的逆袭和互联网+时代(后记)
- sandbox沙盒介绍 以及如何取得沙盒里面各文件的路径,以及xcode5 和xcode6上路径的变化
- ubuntu页面标签切换
- 回到故鄉