(LeetCode 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.
题目要求:
给一个链表和一个数值x,将所有小于x的结点放在所有大于或等于x的结点的前面。
要求不改变原链表结点的相对顺序。
解题思路:
在数组partition中,一般是通过首尾两个指针来进行前后遍历以及交换;
而在链表中,不需要进行元素的交换,可以通过创建两个新的头结点指针,来分别指向小于x的结点和大于等于x的结点,遍历结束之后,再将两个新的链表重新连接起来。
代码:
/**
* 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) {
ListNode *left_head=NULL,*left_tail=NULL;
ListNode *right_head=NULL,*right_tail=NULL;
ListNode *p=head; while(p){
if(p->val<x){
if(left_tail){
left_tail->next=p;
left_tail=left_tail->next;
}
else
left_head=left_tail=p;
}
else{
if(right_tail){
right_tail->next=p;
right_tail=right_tail->next;
}
else
right_head=right_tail=p;
}
p=p->next;
} if(right_tail)
right_tail->next=NULL;
if(left_tail)
left_tail->next=right_head; return left_head?left_head:right_head;
}
};
(LeetCode 86)Partition List的更多相关文章
- (LeetCode 78)SubSets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- LeetCode(86) Partition List
题目 Given a linked list and a value x, partition it such that all nodes less than x come before nodes ...
- 算法学习笔记(LeetCode OJ)
================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...
- 不使用循环或递归判断一个数是否为3的幂(leetcode 326)
326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow ...
- python练习 之 实践出真知 中心扩展法求最大回文子串 (leetcode题目)
1 问题,给定一个字符串,求字符串中包含的最大回文子串,要求O复杂度小于n的平方. 首先需要解决奇数偶数的问题,办法是:插入’#‘,aba变成#a#b#a#,变成奇数个,aa变成#a#a#,变成奇数个 ...
- (LeetCode 41)First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- (LeetCode 153)Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- (LeetCode 160)Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- HDU 4641 K-string 后缀自动机 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=4641 https://blog.csdn.net/asdfgh0308/article/details/4096 ...
- Alpha 冲刺报告6
重感冒,重启中,停工一天
- 【POJ】2796:Feel Good【单调栈】
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 18449 Accepted: 5125 Case T ...
- tyvj 1031 热浪 最短路
热浪 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://tyvj.cn/p/1031 Description 德克萨斯纯朴的民眾们这个夏天正在遭受 ...
- CDOJ 42/BZOJ 2753 滑雪与时间胶囊 kruskal
2753: [SCOI2012]滑雪与时间胶囊 Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1376 Solved: 487[Submit][St ...
- INFORMATION_SCHEMA.COLUMNS-表的字段信息
当前数据库中当前用户可以访问的每一个列在该视图中占一行.INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects.spt_data type_info.systypes.sy ...
- SPOJ 1811. Longest Common Substring (LCS,两个字符串的最长公共子串, 后缀自动机SAM)
1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...
- 关于TFS2010 远程无法创建团队项目的若干问题总结
今天遇到一个TFS的问题,折腾了好几个小时,故将其记录,给有遇到类似问题的朋友一些参考. 1.本文前提:服务器端只安装了TFS2010,本地没有安装Visual Studio 2010,因此不能在服务 ...
- svn提交代码忘写注释怎么办,我想补充上去?
propset --revprop -r 24288--force "svn:log" "一级采购人在填写申报书,汇总批量删除二级采购人申报书时报错" http ...
- tomcat sso 配置
源: http://www.oecp.cn/hi/single/blog/349 为了实现单点登录,做了个CAS SSO单点登录实例,经过反复的琢磨和修改终于成功了,现将CAS SSO单点登录实例详细 ...