题目

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.

代码:oj测试通过 Runtime: 53 ms

 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a ListNode
# @param x, an integer
# @return a ListNode
def partition(self, head, x):
if head is None or head.next is None:
return head h1 = ListNode(0)
dummyh1 = h1
h2 = ListNode(0)
dummyh2 = h2 p = head
while p is not None:
if p.val < x :
h1.next = p
h1 = h1.next
else:
h2.next = p
h2 = h2.next
p = p.next h1.next = dummyh2.next
h2.next = None

思路

这道题的意思就是:把比x小的单拎出来,把大于等于x的单拎出来,再把两个Linked List首尾接起来。

技巧是设定两个虚拟表头dummyh1和dummyh2:保留住两个新表头。

另外需要设定两个迭代变量h1和h2:用于迭代变量

leetcode 【 Partition List 】python 实现的更多相关文章

  1. [leetcode]Partition List @ Python

    原题地址:https://oj.leetcode.com/problems/partition-list/ 题意: Given a linked list and a value x, partiti ...

  2. [LeetCode]题解(python):086 - Partition List

    题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...

  3. [LeetCode]题解(python):131-Palindrome Partitioning

    题目来源: https://leetcode.com/problems/palindrome-partitioning/ 题意分析: 给定一个字符串s,将s拆成若干个子字符串,使得所有的子字符串都是回 ...

  4. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  5. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  6. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  7. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  8. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  9. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  10. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

随机推荐

  1. Echarts图表学习

    最近项目已经运行的比较稳定了,正巧看到ECcharts的图标很炫,遂做一个玩玩,以备后面做数据分析使用. 官网地址:http://echarts.baidu.com/index.html 大致了解了下 ...

  2. Nginx+Keepalived双主轮询负载均衡

    双主模式使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况.同时在前端的DNS服务器对网站做多条A记录,实现了Nginx的负载均衡,当一台服务器故障时候,资源会转移到另 ...

  3. hihocoder第三十六周 二分查找

    题目链接:http://hihocoder.com/contest/hiho36/problem/1 , 一个比较简单的二分. 算法: 由于数据量比较大,O(nlogn)无法通过,所以不能先排序再查找 ...

  4. Sonar服务器搭建

    Sonar服务器搭建 Sonar概述 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson ...

  5. UVA Mega Man's Mission(状压dp)

    把消灭了那些机器人作为状态S,预处理出状态S下可以消灭的机器人,转移统计方案.方案数最多16!,要用64bit保存方案. #include<bits/stdc++.h> using nam ...

  6. SOA体系-三大核心部件

    1.ESB(Enterprise Service Bus)企业服务总线.ESB是传统中间件技术与XML.Web服务等技术结合的产物.ESB提供了网络中最基本的连接中枢,是构筑企业神经系统的必要元素.从 ...

  7. form 表单 和 jQuery HTML / CSS 方法($().html 类似的样式)

    1 有关链接 :http://www.runoob.com/tags/tag-form.html https://www.cnblogs.com/Jxwz/p/4509618.html https:/ ...

  8. 《转载》ASP动态iframe

    原文:[ASP.NET]关于iframe的两个技巧 最近在给朋友写个网站,虽然不大,但是也碰到了一些问题.这篇就为解决ASP.NET中关于IFRAME的两个很现实的问题提供解决方法.PS:呵呵,又做了 ...

  9. docker部署Ceph分布式存储集群

    1.环境准备 3台virtualbox虚拟机,用来安装ceph集群,已用docker-machine安装上了docker,每台虚拟机虚拟创建一个5G的硬盘,用于存储osd数据,例如:/dev/sdb ...

  10. js禁止微信浏览器下拉显示黑底查看网址

    // 首先禁止body document.body.ontouchmove = function (e) { e.preventDefault(); }; // 然后取得触摸点的坐标 var star ...