LeetCode 86. Partition List 链表的划分 Python Solution
来源:互联网 发布:怎么在淘宝上买军刺 编辑:程序博客网 时间:2024/05/16 09:39
此题目对应于86. Partition List
题目要求基于一个给定的值x对链表做一个划分partition,使得小于x的放在前,大于等于x的放在后,两部分各自保持原有顺序。
解题思路很简单:
1.设置两个链表,一个存放小于x的元素,一个存放大于等于x的元素
2.合并两个链表,这里需要注意的是需要对第二个辅助链表的表尾进行制空,以防止出现从头到尾的循环链表。
下面附上python代码:
# -*- coding: utf-8 -*-# Definition for singly-linked list.class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): #给个list,给个值,把list分为两部分,小于x的放在前,大于等于x的放在后,两部分各自保持原有顺序 def partition(self, head, x): if not head or not head.next: return head pre1 = ListNode(0) cur1 = pre1 pre2 = ListNode(0) cur2 = pre2 while head: if head.val < x: cur1.next = head cur1 = cur1.next else: cur2.next = head cur2 = cur2.next head = head.next cur1.next = pre2.next cur2.next = None#需要制空否则会形成从头到尾的一致循环的list return pre1.next """ :type head: ListNode :type x: int :rtype: ListNode """
阅读全文
0 0
- LeetCode 86. Partition List 链表的划分 Python Solution
- Leetcode 86. Partition List(链表划分)
- Partition List 链表划分区间@LeetCode
- LeetCode(Partition List) 划分链表
- 【LeetCode】Partition List 链表划分
- leetcode 86. Partition List-链表划分|双指针
- Leetcode 86. Partition List 链表划分 解题报告
- LeetCode OJ 之 Partition List (划分链表)
- [LeetCode 86] Partition List Solution
- LintCode Partition List 链表划分
- Partition List | LeetCode 8ms C++Solution
- partition List Leetcode Python
- [Leetcode][python]Partition List
- 【LeetCode with Python】 Partition List
- 【LEETCODE】86- Partition List [Python]
- [LeetCode]86.Partition List
- LeetCode 86. Partition List
- [Leetcode] 86. Partition List
- 复杂链表的复制
- Git 简单使用学习
- laravel学习笔记之Eloquent ORM
- 正则匹配小知识
- Google 和 Levi's 合作的智能夹克终于要开卖了
- LeetCode 86. Partition List 链表的划分 Python Solution
- spring初始化异常ClassVisitor as super class
- C#操作Excel(三)相关函数
- django中过滤器 | 的运用
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(九)版本控制
- 利用Spring boot如何创建简单的web交互应用
- IE浏览器兼容jsp问题
- 解决jQuery聚焦时光标在input最前面的问题
- 树莓派装mysql