[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 ...
随机推荐
- [代码审计]yxcms从伪xss到getshell
0x00 前言 这篇文章首发于圈子,这里作为记录一下. 整个利用链构造下来是比较有趣的,但实际渗透中遇到的几率比较少. 此次审的是yxcms 1.4.6版本,应该是最后一个版本了吧? 0x01 从任意 ...
- mysql创建索引笔记
1.添加PRIMARY KEY(主键索引.就是 唯一 且 不能为空.): ALTER TABLE `table_name` ADD PRIMARY KEY ( `column` ) 2.添加UNIQU ...
- android 的安全问题
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 1,webView的js脚本引发的安全 2,代码的混淆加密.还可采用第三方apk加固程序 ...
- RelativeSource的用法
绑定自身的数据 <Style TargetType="Button"> <Setter Property="Width" Value=&quo ...
- BZOJ3500 : PA2008 Cliquers
设g[i]表示n=i时的答案,则OEIS上可以找到如下递推式: g[i]=g[i-1]+g[i-2]-g[i-5]-g[i-7]+... 其中符号为++--交替,第i项为f[i],f[1]=1,f[2 ...
- 无线端安全登录与鉴权一之Kerberos
无线端登录与鉴权是安全登录以及保证用户数据安全的第一步,也是最重要的一步.之前做过一个安全登录与鉴权的方案,借这个机会,系统的思考一下,与大家交流交流 先介绍一下TX系统使用的Kerberos方案,参 ...
- LPC43xx Asymmetric Dual Core : Cortex-M0 and Cortex-M4
- 添加类似navigationController自带的返回按钮
添加类似navigationController自带的返回按钮,效果如下: 一.UINavigationcontroller自带的navigationBar 是无法添加左箭头的返回按钮的 在网上搜索了 ...
- Android 自动编译、打包生成apk文件 2 - 使用原生Ant方式
from://http://blog.csdn.net/androiddevelop/article/details/11100109 相关文章列表: <Android 自动编译.打包生成apk ...
- JavaScript:动态代理之远程代理
背景 2008 第一次使用 AJAX 的时候好像使用的是 AJAX.NET,该组件支持为服务器 C# 类型提供 JS 代理,当时不是十分的明白.设计模式中有代理模式,不过真正需要我们手工写代理的次数却 ...