题目:

给定一个单链表和数值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 链表划分的更多相关文章

  1. lintcode 中等题:partition array 数组划分

    题目 数组划分 给出一个整数数组nums和一个整数k.划分数组(即移动数组nums中的元素),使得: 所有小于k的元素移到左边 所有大于等于k的元素移到右边 返回数组划分的位置,即数组中第一个位置i, ...

  2. 2.4---把链表划分为两部分(CC150)

    注意,题目要求要保持两部分的相对顺序,所以,用交换是不行的. import java.util.HashSet; import java.util.Set; class ListNode{ int v ...

  3. 【LeetCode】Partition List ——链表排序问题

    [题目] Given a linked list and a value x, partition it such that all nodes less than x come before nod ...

  4. LintCode之删除链表中的元素

    题目描述 我的代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n ...

  5. 【LintCode】删除链表中的元素

    问题分析: 声明当前指针和上一个指针即可. 问题求解: public class Solution { public ListNode removeElements(ListNode head, in ...

  6. Lintcode: Partition Array

    Given an array "nums" of integers and an int "k", Partition the array (i.e move ...

  7. lintcode:删除链表中指定元素

    题目 删除链表中等于给定值val的所有节点. 样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1-> ...

  8. lintcode:交换链表当中两个节点

    题目 给你一个链表以及两个权值v1和v2,交换链表中权值为v1和v2的这两个节点.保证链表中节点权值各不相同,如果没有找到对应节点,那么什么也不用做. 注意事项 你需要交换两个节点而不是改变节点的权值 ...

  9. lintcode:带环链表

    带环链表 给定一个链表,判断它是否有环. 解题 定义两个指针p1 p2 p1每次向前走一步 p2每次向前走两步 当p2能赶上p1的时候说明有环 /** * Definition for ListNod ...

随机推荐

  1. C# 带进度条的文件下载

    private long fileLength; private long downLength;//已经下载文件大小,外面想用就改成公共属性 private static bool stopDown ...

  2. JS遇到的问题解决

    1.input 里使用onclick事件,整整花了1个半小时,onclick里面直接用Location.href不需要加<script></script> <?php / ...

  3. linux修改挂载目录

    linux修改挂载目录   修改扩展磁盘默认的挂载点/home到/data   [root@localhost ~]# df -h 文件系统       容量  已用  可用 已用%% 挂载点 /de ...

  4. Global::time2StrHHMM_DNT

    /*************************************************** Created Date: 13 Jul 2013 Created By: Jimmy Xie ...

  5. 第一次比赛的 C题 (好后面才补的....) CodeForces 546B

    Description Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge ...

  6. Setting composer minimum stability for your application

    Do you have a confusion of how do you determine the stability when using composer dependency manager ...

  7. 解决VS2012新建MVC3等项目时,收到加载程序集“NuGet.VisualStudio.Interop…”的错误

    vs2012来做一个mvc3的项目,哪知在创建ado数据模型时跳出这么一个东东 错 误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0 ...

  8. kruskal --- C++

    #include <cstdio> #include <algorithm> using namespace std; struct aaa{ int l,r,w; bool ...

  9. 谁是最好的Coder

    谁是最好的Coder 时间限制:1000 ms  |  内存限制:65535 KB 难度:0   描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所以他选了帅 ...

  10. .net datatable 添加一列

    dt.Columns.Add("image", Type.GetType("System.String")); foreach (DataRow dr in d ...