leetcode 链表 Partition List
Partition List
Total Accepted: 19761 Total
Submissions: 73252My Submissions
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小的节点的链表l1,还有一个是大于等于x的节点的链表l2
最后把l1的尾部接到l2的头部
复杂度:时间O(n)。空间O(1)
ListNode *partition(ListNode *head, int x) {
ListNode less(-1), larger_equal(-1);
ListNode *less_ptr = &less, *larger_equal_ptr = &larger_equal;
for (ListNode *cur = head; cur != NULL; cur = cur->next)
{
if(cur->val < x){
less_ptr = less_ptr->next = cur;
}else{
larger_equal_ptr = larger_equal_ptr->next = cur;
}
}
less_ptr->next = larger_equal.next;
larger_equal_ptr->next = NULL;
return less.next;
}
leetcode 链表 Partition List的更多相关文章
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- [LeetCode] [链表] 相关题目总结
刷完了LeetCode链表相关的经典题目,总结一下用到的技巧: 技巧 哑节点--哑节点可以将很多特殊case(比如:NULL或者单节点问题)转化为一般case进行统一处理,这样代码实现更加简洁,优雅 ...
- Leetcode链表
Leetcode链表 一.闲聊 边学边刷的--慢慢写慢慢更 二.题目 1.移除链表元素 题干: 思路: 删除链表节点,就多了一个判断等值. 由于是单向链表,所以要删除节点时要找到目标节点的上一个节点, ...
- [LeetCode] 86. Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- LeetCode 86. 分隔链表(Partition List)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- LeetCode 86. Partition List 划分链表 C++
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- [leetcode]86. Partition List划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【LeetCode】Partition List ——链表排序问题
[题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...
- [LeetCode]86. Partition List分离链表
/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(L ...
随机推荐
- Eclipse中项目进行发布到Tomcat中的位置
Eclips配置tomcat默认是发布到.metadate\plugins\目录下的,wtpwebapps.这样在实际的tomcat目录下,就找不到发布的项目.我们可以自己进行设置,在控制栏中找到se ...
- angularjs 模块化
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- iOS菜鸟成长笔记(2)——网易彩票练习
距离上一篇<第一个iOS应用>已经有一个多月了,今天来和大家一起学习和分享一下一个小练习<网易彩票> 首先我们向storyboard中拖入一个TabBarController和 ...
- windows下MySQL5.6以上版本,如何通过修改配置文件来修改数据库的最大连接数啊?
并没有my.ini文件,只有一个my-default.ini文件,并且里面并没有max_connections windows下MySQL5.6以上版本,如何通过修改配置文件来修改数据库的最大连接数啊 ...
- <Sicily>Pythagorean Proposition
一.题目描述 One day, WXYZ got a wooden stick, he wanted to split it into three sticks and make a right-an ...
- windows, fast-rcnn CPU版本的安装配置
一:安装准备 1:caffe的安装配置,本人用的是happynear大神的caffe版本,具体链接https://github.com/happynear/caffe-windows,编译时需要用到p ...
- linux 下 .sh 文件语法
转自:http://blog.sina.com.cn/s/blog_54f82cc201010hfz.html 介绍: 1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh ...
- JDBC连接SQL Server遇到的问题
需要使用到微软的JDBC sql server的驱动类,去官网下载jar包 使用的URL模式:"jdbc:sqlserver:地址:端口//;databaseName=YourDatabas ...
- Java基础学习总结(26)——JNDI入门简介
JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一,不少专家认为,没有透彻理解JNDI的意义和作用,就没有 ...
- springMVC的rest风格的url请求
rest是一个架构风格,用url来访问网络上的任何资源.rest的一种思想就是用http中的动作get,post,put,delete,来进行增删改查. 这里介绍的是springMVC的rest请求. ...