lintcode :Partition List 链表划分
题目:
给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。
你应该保留两部分内链表节点原有的相对顺序。
给定链表 1->4->3->2->5->2->null,并且 x=3
返回 1->2->2->4->3->5->null
解题:
上面返回的结果好多不对的应该是: 1->2->2->3->4->5->null
想了好久不知道怎么搞,九章看到的程序,竟然搞两个链表链接起来。。。
Java程序:
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The first node of linked list.
* @param x: an integer
* @return: a ListNode
*/
public ListNode partition(ListNode head, int x) {
// write your code here
if(head == null) return null;
ListNode leftDummy = new ListNode(0);
ListNode rightDummy = new ListNode(0);
ListNode left = leftDummy, right = rightDummy; while (head != null) {
if (head.val < x) {
left.next = head;
left = head;
} else {
right.next = head;
right = head;
}
head = head.next;
} right.next = null;
left.next = rightDummy.next;
return leftDummy.next; } }
总耗时: 1573 ms
Python程序:
"""
Definition of ListNode
class ListNode(object): def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of linked list.
@param x: an integer
@return: a ListNode
"""
def partition(self, head, x):
# write your code here
if head == None:
return head
lefthead = ListNode(0)
righthead = ListNode(0)
left = lefthead
right = righthead
while head!=None:
if head.val<x:
left.next = head
left = left.next
else:
right.next = head
right = right.next
head = head.next
right.next = None
left.next = righthead.next
return lefthead.next
总耗时: 517 ms
和Java的有点小区别但是没有影响的。
lintcode :Partition List 链表划分的更多相关文章
- lintcode 中等题:partition array 数组划分
题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...
- 2.4---把链表划分为两部分(CC150)
注意,题目要求要保持两部分的相对顺序,所以,用交换是不行的. import java.util.HashSet; import java.util.Set; class ListNode{ int v ...
- 【LeetCode】Partition List ——链表排序问题
[题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...
- LintCode之删除链表中的元素
题目描述 我的代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n ...
- 【LintCode】删除链表中的元素
问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, in ...
- Lintcode: Partition Array
Given an array "nums" of integers and an int "k", Partition the array (i.e move ...
- lintcode:删除链表中指定元素
题目 删除链表中等于给定值val的所有节点. 样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1-> ...
- lintcode:交换链表当中两个节点
题目 给你一个链表以及两个权值v1和v2,交换链表中权值为v1和v2的这两个节点.保证链表中节点权值各不相同,如果没有找到对应节点,那么什么也不用做. 注意事项 你需要交换两个节点而不是改变节点的权值 ...
- lintcode:带环链表
带环链表 给定一个链表,判断它是否有环. 解题 定义两个指针p1 p2 p1每次向前走一步 p2每次向前走两步 当p2能赶上p1的时候说明有环 /** * Definition for ListNod ...
随机推荐
- UVA10361 -自动作诗机
UVA10361 - Automatic Poetry(自动作诗机) A Schuttelreim seems to be a typical German invention. The funny ...
- 所有外包项目威客网站列表----来自程序员接私活网qxj.me
猪八戒 http://www.zhubajie.com/ 有佣金,建议别去坑死了 csto http://www.csto.com/ 开源中国众包 https://zb.osch ...
- with check option(视图 )
建立视图的时候使用WITH CHECK OPTION 与不使用的区别 WITH CHECK OPTION insert update delete 使用with check option 保证ins ...
- Python学习指南
学习python书籍&资料: 1. Python v2.7.5 documentation 2. [Python参考手册(第4版)].(美)比兹利.扫描版.pdf 3. [Python技术手册 ...
- How to install DIG dns tool on windows 7
This guide explain how to install dig dns tool on windows 7 in few steps: 1. First go to http://www. ...
- vagrant在windows下的使用
vagrant在windows下的使用 下载安装 VirtualBox :https://www.virtualbox.org/ 下载安装 Vagrant :http://www.vagrantup. ...
- Access
一般系统的实现: 管理系统的分析与设计 --->>数据表的设计创建 --->> 设计“查询”与“宏” --->> 创建窗体与报表 --->>系统注册 启 ...
- 例题-文件系统的放大 (LVM):
纯粹假设的,我们的 /home 不够用了,你想要将 /home 放大到 7GB 可不可行啊? 答: 因为当初就担心这个问题,所以 /home 已经是 LVM 的方式来管理了.此时我们要来瞧瞧 VG 够 ...
- 2877: [Noi2012]魔幻棋盘 - BZOJ
DescriptionInput 第一行为两个正整数N,M,表示棋盘的大小. 第二行为两个正整数X,Y,表示棋盘守护者的位置. 第三行仅有一个正整数T,表示棋盘守护者将进行次操作. 接下来N行,每行有 ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror] B. Bribes lca
题目链接: http://codeforces.com/contest/575/problem/B 题解: 把链u,v拆成u,lca(u,v)和v,lca(u,v)(v,lca(u,v)是倒过来的). ...