【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 实现 ...
随机推荐
- 洛谷 1328 生活大爆炸版石头剪刀布(NOIp2014提高组)
[题解] 简单粗暴的模拟题. #include<cstdio> #include<algorithm> #include<cstring> #define LL l ...
- 对百词斩&可可英语的测试
第六周小组作业 基本任务:功能测试和测试管理 温馨提示:本篇博客中,看不清的图片,可以按住Ctrl同时滚动鼠标滚轮查看:也可以点击图片下方的链接,在新选项卡打开后,点击小加号查看. (1)计划说明 本 ...
- PatentTips - Method for booting a host device from an MMC/SD device
FIELD OF THE INVENTION The present invention relates to a memory device and especially to the interf ...
- 非常适合新手的jq/zepto源码分析06 -- 事件模型
复习下事件的有关内容: 1.现在用的绑定/删除: obj.addEventListener(type,fn,false) obj.removeEventListener(type) obj.attac ...
- RMAN RECOVERY
Data Recovery Advisor The health monitor and the ADR The capabilities and limitations of DRA using t ...
- docker: useful commands
docker build -t stock_data_repo_instance24 . docker run -v /opt/log:/opt/log -d -it stock_data_repo_ ...
- 又通过一道题目,替换字符串 —— 剑指Offer
https://www.nowcoder.net/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage= ...
- Domino V8 在 UNIX/Linux 平台上的安装及其常见问题
在 IBM Bluemix 云平台上开发并部署您的下一个应用. 开始您的试用 Domino V8 的安装需求 Domino V8 可以支持多种平台和操作系统,表1 列出了其支持的各种 UNIX/Lin ...
- [Vue @Component] Handle Errors and Loading with Vue Async Components
Because async components are not bundled with your app, they need to be loaded when requested. This ...
- UIView的层介绍
UIView的层介绍 subview在西安市到屏幕上时,是位于superview上层的. 同一个view的subview时依照增加的顺序显示相应层的.越晚增加的subview显示在越上层,反之也是如此 ...