【LEETCODE】86- Partition List [Python]
2016-02-26 10:46
543 查看
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.
题意:
给个list,给个值,把list分为两部分,小于x的放在前,大于等于x的放在后,两部分各自保持原有顺序
思路:
use p to move from head to end and compare each value with x
create two dummy point, one is used to link points that are <x, another is to link points that are >=x
Python:
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.
题意:
给个list,给个值,把list分为两部分,小于x的放在前,大于等于x的放在后,两部分各自保持原有顺序
思路:
use p to move from head to end and compare each value with x
create two dummy point, one is used to link points that are <x, another is to link points that are >=x
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 is None or head.next is None or x is None: return head p1=head1=ListNode(0) p2=head2=ListNode(0) p=head while p: if p.val<x: p1.next=p p1=p1.next else: p2.next=p p2=p2.next p=p.next p1.next=head2.next p2.next=None return head1.next
相关文章推荐
- pycharm修改默认__author__ = '$USER'
- python dlib、opencv 配置
- python系列之 - 面向对象(一)
- 8.python模块补充
- 【python】编程语言入门经典100例--13
- Python基础0226
- 15个最受欢迎的Python开源框架
- 用python访问URL
- Python的常量
- 【python】编程语言入门经典100例--12
- Python Note
- 用Scrapy爬取教务处通知公告
- 我们编程吧 之 python 学习手册0.75
- python3 学习笔记
- Python图像处理库PIL的ImageDraw模块介绍
- 我的Python成长之路---第六天---Python基础(18)---2016年2月20日(晴)
- Python字符串的encode与decode研究心得乱码问题解决方法
- python写一个爬虫(4)改进
- Python学习笔记day6
- 简析Python的闭包和装饰器