题目

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. Android Broadcast Receive

    Broadcast Receive 广播接收(Broadcast Receive)为android的四大组件之一.主要用于监听广播消息,并做出响应.与应用程序中监听事件相比而言,该监听事件为全局监听. ...

  2. JS案例练习 — 给div添加样式选择功能

    附加效果图: CSS内容: <style> ; padding:0px} li{list-style:none} body{font:24px 'Microsoft YaHei'; col ...

  3. php-7.1.11编译选项配置

    ./configure \ --prefix=/usr/local/php-7.1.11 \ --with-config-file-path=/usr/local/php7.1.11/etc \ -- ...

  4. MVC+Nhibernate+jquery+easyui递归实现多级菜单

    1.新建访问的控制器动作返回视图,在视图中使用easyui的treegrid插件来得到后台得到的json数据显示多级菜单 public ActionResult Menu() { return Vie ...

  5. win10搜索不到蓝牙设备

    多半是驱动不兼容的问题. 解决方法: 此电脑右键,设备管理器,然后将蓝牙下的驱动,右键.卸载设备. 安装驱动精灵,会自动检测到缺少蓝牙驱动,安装即可.

  6. IOS 获取文本焦点 主动召唤出键盘(becomeFirstResponder) and 失去焦点(退下键盘)

    主动召唤出键盘 - (void)viewDidAppear:(BOOL)animated { // 3.主动召唤出键盘 [self.nameField becomeFirstResponder]; / ...

  7. Aizu 2303 Marathon Match (概率)

    因为第i个人休息j次服从二项分布,算一下组合数. 数据范围小. 求出第i个人休息j次的概率和对应的时间之后,全概率公式暴力统计. #include<bits/stdc++.h> using ...

  8. 【洛谷1967】货车运输(最大生成树+倍增LCA)

    点此看题面 大致题意: 有\(n\)个城市和\(m\)条道路,每条道路有一个限重.多组询问,每次询问从\(x\)到\(y\)的最大载重为多少. 一个贪心的想法 首先,让我们来贪心一波. 由于要求最大载 ...

  9. 【洛谷2633】Count on a tree(树上主席树)

    点此看题面 大致题意: 给你一棵树,每次问你两点之间第\(k\)小的点权,强制在线. 主席树 这种题目强制在线一般就是数据结构了. 而看到区间第\(k\)小,很容易就能想到主席树. 至少不会有人想到树 ...

  10. 跑rbgirshick的fast-rcnn代码

    需要安装Caffe.pycaffe cython.python-opencv.easydict matlab(主要用于对PASCALvoc数据集的评估) 为什么要bulid cython.caffe. ...