[leetcode]Partition List @ Python
原题地址:https://oj.leetcode.com/problems/partition-list/
题意:
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.
解题思路:解决链表问题时,最好加一个头结点,问题会比较好解决。对这道题来说,创建两个头结点head1和head2,head1这条链表是小于x值的节点的链表,head2链表是大于等于x值的节点的链表,然后将head2链表链接到head链表的尾部即可。
代码:
# 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):
head1 = ListNode(0)
head2 = ListNode(0)
Tmp = head
phead1 = head1
phead2 = head2
while Tmp:
if Tmp.val < x:
phead1.next = Tmp
Tmp = Tmp.next
phead1 = phead1.next
phead1.next = None
else:
phead2.next = Tmp
Tmp = Tmp.next
phead2 = phead2.next
phead2.next = None
phead1.next = head2.next
head = head1.next
return head
[leetcode]Partition List @ Python的更多相关文章
- [LeetCode]题解(python):086 - Partition List
题目来源 https://leetcode.com/problems/partition-list/ Given a linked list and a value x, partition it s ...
- [LeetCode]题解(python):131-Palindrome Partitioning
题目来源: https://leetcode.com/problems/palindrome-partitioning/ 题意分析: 给定一个字符串s,将s拆成若干个子字符串,使得所有的子字符串都是回 ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [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]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- [LeetCode]题解(python):118 Pascal's Triangle
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...
- [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 ...
随机推荐
- EntityFramework笔记
参照文档:http://www.cnblogs.com/farb/p/ABPAdvancedTheoryContent.html 案例:http://pan.baidu.com/s/1c1Qgg28 ...
- Android中线程通信的方式
Android 跨线程通信 android 中是不允许在主线程中进行 网络访问等事情的因为UI如果停止响应5秒左右的话整个应用就会崩溃,到Android4.0 以后 Google强制规定,与网络相关的 ...
- CO文件升级
当在Process Designer文件中导入旧版本CO模型时,拖入Graphic View后会出现如下错误.升级CO文件可解决该错误. 使用开始菜单中Tecnomatix下的Update to ...
- [POI2013]Usuwanka
[POI2013]Usuwanka 题目大意: 一排\(n\)个球,有黑白两种颜色.每取走一个球会在原位置放一个水晶球.求构造一种取球方案,满足: 每次取走\(k\)个白球和\(1\)个黑球: 一次取 ...
- [USACO06JAN]Redundant Paths
OJ题号:洛谷2860.POJ3177 题目大意: 给定一个无向图,试添加最少的边使得原图中没有桥. 思路: Tarjan缩点,然后统计度为$1$的连通分量的个数(找出原图中所有的桥). 考虑给它们每 ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- RubyMine8 安装
在win7下面安装RubyMine8 中文 汉化 1.需要下载 RubyMine8的程序包 2.先安装好 RubyMine8 很简单 点击下一步就可以 选择安装目录这应该就不需要说了 3.汉化 ...
- LPC1800 and LPC4300 MCUs
LPC1800 Series microcontrollers At 180 MHz, LPC1800 Series microcontrollers combine the industry's f ...
- LPCScrypt, DFUSec : USB FLASH download, programming, and security tool, LPC-Link 2 Configuration tool, Firmware Programming
What does this tool do? The LPC18xx/43xx DFUSec utility is a Windows PC tool that provides support f ...
- 零宽断言 -- Lookahead/Lookahead Positive/Negative
http://www.vaikan.com/regular-expression-to-match-string-not-containing-a-word/ 经常我们会遇到想找出不包含某个字符串的文 ...