【LeetCode】86. Partition List 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/partition-list/description/

题目描述:

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.

Example:

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5

题目大意

要把一个单链表中,小于某个数字的所有节点放到前面,其余的位置都不要变化。

解题方法

乍一看,感觉原地做这些操作很难。但是,我们换个思路就感觉很简单了。

做链表的题,不要省指针。

用两个新指针,分别记录比x值小的和比x值大的,遍历原来的链表的时候根据值的大小拼接到对应的链表后面。

最后再把两个链表拼接到一起就行了。

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None class Solution(object):
def partition(self, head, x):
"""
:type head: ListNode
:type x: int
:rtype: ListNode
"""
small = ListNode(0)
large = ListNode(0)
small_root, large_root = small, large
while head:
if head.val < x:
small.next = head
small = small.next
else:
large.next = head
large = large.next
temp = head.next
head.next = None
head = temp
small.next = large_root.next
return small_root.next

日期

2018 年 6 月 24 日 ———— 今天请客吃了海底捞~

【LeetCode】86. Partition List 解题报告(Python)的更多相关文章

  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 ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. 如何使用csapp文件

    深入理解操作系统中有个csapp.h的头文件 以下来介绍下如何使用它: 该头文件下载地址为http://download.csdn.net/detail/tzasd89812/4206284 在Ubu ...

  2. [源码解析] PyTorch分布式优化器(1)----基石篇

    [源码解析] PyTorch分布式优化器(1)----基石篇 目录 [源码解析] PyTorch分布式优化器(1)----基石篇 0x00 摘要 0x01 从问题出发 1.1 示例 1.2 问题点 0 ...

  3. 1005.K次取反后最大化的数组和

    1005.K次取反后最大化的数组和 目录 1005.K次取反后最大化的数组和 题目 题解 排序+维护最小值min 题目 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 ...

  4. ache

    ache和pain可能没啥差别,头疼和头好痛都对.从词典来看,有backache, bellyache, earache, headache, heartache, moustache/mustach ...

  5. 自定义控件CustomAlertView

    [记录][完整代码最下] 效果如下: 可行性分析: 由于系统自带的UIAlertView样式简单,只有两种样式,想要理想的样式就要自定义控件了 文件名取为:CustomAlertView 创建文件如下 ...

  6. java poi导出多sheet页

    /** * @Title: exportExcel * @Description: 导出Excel的方法 * @param workbook * @param sheetNum (sheet的位置,0 ...

  7. OpenStack之五: image镜像服务(端口9292)

    官网地址:https://docs.openstack.org/glance/stein/install/install-rdo.html #:创建glance库,并授权 MariaDB [(none ...

  8. binlog浅析

    binlog浅析 一.基础知识 什么是binlog? (图一) 全称:Binary Log (二进制日志),包含描述数据库更改的" 事件 ",例如表创建操作或对表数据的更改.二进制 ...

  9. Charles 手机抓包

    Charles 手机抓包 请求抓包对于程序员调试代码必不可少,Charles是一个用与抓包的好工具(也可以使用Fiddler),Charles抓包是通过中间人代理实现,在客户端和服务端通信时,Char ...

  10. 为什么重写equals()就要重写hashcode()

    阿里巴巴开发规范 只要重写 equals,就必须重写 hashCode 因为 Set 存储的是不重复的对象,依据 hashCode 和 equals 进行判断,所以 Set 存储的对象必须重写这两个方 ...