【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 实现 ...
随机推荐
- 2018百度之星资格赛T2 子串查询
[题解] 很容易想到暴力做法:对于每个询问暴力查找区间内的最小字母,统计其出现次数.效率O(N^2),无法通过全部数据. 我们可以换一个思路,设f[i][j]为第i个字母(字母‘A'到’Z'分别对应0 ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- POJ 2115 简单的模线性方程求解
简单的扩展欧几里得题 这里 2^k 不能自作聪明的用 1<<k来写 , k >= 31时就爆int了 , 即使定义为long long 也不能直接这样写 后来老老实实 for(int ...
- zoj 3812 状压dp
转载:http://blog.csdn.net/qian99/article/details/39138329 题意:给出n个物品,每个物品有两种属性Wi,Ti,有q组查询,每组查询要求在n个物品中选 ...
- UVa - 12451 - Let's call SPaDe a SPaDe
先上题目: Problem H: Let's call SPaDe a SPaDe Passing time, walking the passage, as you pass the String ...
- FFT模板(From MG)
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; struct c ...
- C - How Many Tables 并查集
Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius wants to kn ...
- Ubuntu 16.04安装VMware-Workstation-12
1.下载: https://download3.vmware.com/software/wkst/file/VMware-Workstation-Full-12.1.1-3770994.x86_64. ...
- 第6章 TCP/IP路由协议故障处理
第6章 TCP/IP路由协议故障处理 一.缺省网关 当包的目的地址不在路由器的路由表中,如路由器配置了缺省网关,则转发到缺省网关,否则就丢弃. Show ip route :查看Cisco路由器的缺省 ...
- install yael on the ubuntu 12.04
1. bits/predefs.h no such file or directory ??? sudo apt-get install gcc-multilib 2. sudo gedit /et ...