【Leetcode 86】 Partition List
问题描述:
给定一个list, 将所有小于x的node放到左边,剩下的保持原样。
问题解决:
闲的无聊,用c++和python都做了一遍。
代码如下:
# 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
"""
if(head == None):
return head
p = head
lessList = None
largerList = None
newHead = None
lHead = None
while(p):
print(p.val)
if(p.val < x):
if(newHead == None):
newHead = p
else:
lessList.next = p
lessList = p
else:
if(lHead == None):
lHead = p
else:
largerList.next = p
largerList = p
t = p.next
p.next = None
p = t
if(newHead == None):
newHead = lHead
else:
lessList.next = lHead
return newHead
第一个py,解决了个小问题。加油吧~~
【Leetcode 86】 Partition List的更多相关文章
- 【LeetCode练习题】Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【LeetCode题解】530_二分搜索树的最小绝对值差
目录 [LeetCode题解]530_二分搜索树的最小绝对值差 描述 方法一.中序遍历二分搜索树 思路 Java 代码 Python 代码 [LeetCode题解]530_二分搜索树的最小绝对值差 描 ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- 【LeetCode练习题】Permutation Sequence
Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 【LeetCode题解】136_只出现一次的数字
目录 [LeetCode题解]136_只出现一次的数字 描述 方法一:列表操作 思路 Java 实现 Python 实现 方法二:哈希表 思路 Java 实现 Python 实现 方法三:数学运算 思 ...
- 【LeetCode题解】7_反转整数
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...
- 【LeetCode题解】350_两个数组的交集Ⅱ
目录 [LeetCode题解]350_两个数组的交集Ⅱ 描述 方法一:映射 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 Python 实现 [Lee ...
- 【LeetCode题解】349_两个数组的交集
目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 ...
随机推荐
- F - Shooter
UVA___10535 The shooter is in a great problem. He is trapped in a “2D” maze with a laser gun and can ...
- jmeter 性能测试
1. Ramp-up Period(in seconds)代表多长时间内启动所有线程 2. Aggregate Report Samples:总共发给服务器的请求数量 Average:单个请求的平均响 ...
- 关于字符串不为空 错误:s!=null
错误:s!=null 正确:StringUtils.isNotBlank(s); public static boolean isBlank(CharSequence cs) { int strLen ...
- scrapy——中间件UserAgent代理
pip install fake-useragent 使用说明:from fake_useragent import UserAgent# 实例化一个UserAgent对象ua = UserAgent ...
- 高数(A)下 第十章
10.1 10.2 10.3 10.4 10.5 10.7 自测题
- Ubuntu 16.04安装indicator-sysmonitor实现导航条显示上下行网速/CPU/内存使用率
安装: sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor sudo apt-get update sudo apt-get in ...
- iOS消息推送原理和实现总结
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图:1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Provi ...
- kfk: async disk IO深度解析
http://www.itpub.net/thread-1724044-1-1.html
- pojo类对应的就是数据库中的表,pojo类属性类型一定要用包装类Integer等
pojo类对应的就是数据库中的表,pojo类属性类型一定要用包装类Integer等 pojo类对应的就是数据库中的表,pojo类属性类型一定要用包装类Integer等 pojo类对应的就是数据库中的表 ...
- CentosOS 7: 创建Nginx+Https网站
参考文章: 1. https://github.com/Neilpang/acme.sh/wiki/%E8%AF%B4%E6%98%8E 2. http://songchenwen.com/tech/ ...