(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 ...
随机推荐
- BZOJ 5059: 前鬼后鬼的守护 可并堆 左偏树 数学
https://www.lydsy.com/JudgeOnline/problem.php?id=5059 题意:将原序列{ai}改为一个递增序列{ai1}并且使得abs(ai-ai1)的和最小. 如 ...
- Apache之.htaccess备忘录(二)
博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助的可以关注我. 转载请注明"深蓝的镰刀" 书接上回,<Apache之.htaccess备忘录(一)& ...
- wikioi 1048 石子归并
dp[i][j]=min(dp[i][j],dp[i][k],dp[k+1][j]+sum[i][j]); 表示i-j的最小合并代价. #include <iostream> #inclu ...
- faceNet编译问题
1.执行align_dataset_mtcnn.py出现无法导入检测模型的问题 a.现象如下 Creating networks and loading parameters Traceback (m ...
- 改变 Windows 用户文件夹默认位置
https://social.microsoft.com/Forums/zh-CN/b6602d50-1a75-42d2-90bd-3dc6c2c82147/programdata?forum=win ...
- Oracle中空值与数字相加问题
select 10 + 10 + 10 from dual 结果是30,全然没问题. select null + 10 + 10 from dual 结果是空串,但期望的结果是20. select n ...
- iOS 调用短信、电话、邮件、浏览器等
1.调用 自带mail[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://admin@hzl ...
- python笔记30-docstring注释添加变量
前言 python里面添加字符串注释非常简单,如何将变量放入 python 的函数注释里面呢? docstring也就是给代码加注释的内容了,python可以给函数,类.方法,模块添加注释内容,注释标 ...
- JDBC连接oracle RAC数据库配置
RAC的配置如下: node1:ip地址192.168.60.132,实例名:rac1,主机名:rac1 node2:ip地址192.168.60.144,实例名:rac2,主机名:rac2 RAC服 ...
- Excel向上取整
CEILING函数语法:CEILING(number,significance)