【leetcode】86. 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.
Example:
Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5
解题思路:我的方法和【leetcode】328. Odd Even Linked List 一样,使用less 和 great 两个指针,分别指向小于和等于或大于的节点,最后再把less的尾节点指向great的头结点即可。
代码如下:
# Definition for singly-linked list. class ListNode(object):def __init__(self, x):self.val = xself.next = Noneclass Solution(object):def partition(self, head, x):""":type head: ListNode:type x: int:rtype: ListNode"""lessHead = less = greatHead = great = Nonecurrent = headwhile current != None:tmp = current.nextcurrent.next = Noneif current.val < x:if lessHead == None:lessHead = currentless = lessHeadelse:less.next = currentless = less.nextelse:if greatHead == None:greatHead = great = currentelse:great.next = currentgreat = great.nextcurrent = tmpif less != None:less.next = greatHeadelse:lessHead = greatHeadreturn lessHead
转载于:https://www.cnblogs.com/seyjs/p/10194292.html
【leetcode】86. Partition List相关推荐
- 【LeetCode】86. 分隔链表
86. 分隔链表 题解 这题不难,事先准备两个链表,遍历题目给的链表,将小于x和大于等于x的两类数据分别放到准备好的链表中,然后两个链表拼接即可: 注意点: 链表在创建增加的时候,游走的那个指针在当前 ...
- 【leetcode】416. Partition Equal Subset Sum
题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...
- 【leetcode】解题日记(未完待续)
开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题
[LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...
- 【Leetcode】100. 相同的树
题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...
- 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵
1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
- 【leetcode】132. Palindrome Partitioning II
题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...
最新文章
- 用javascript实现一门编程语言-字符输入流
- java中对象的生存期_JSP中JavaBean的生命周期
- RKNN Toolkit使用教程
- SAP使用表空间传输异构系统迁移
- 【Python基础入门系列】第04天:Python 流程控制
- 聚类的概念和一般步骤
- 记一次MBR锁机病毒分析
- Python数据挖掘笔记 七 .PCA降维操作及subplot子图绘制
- 读书感受 之 《穷查理宝典》
- 前端工程师面试时自我介绍该如何做?
- 亚丁号云控之发布任务
- Visual SourceSafe Explorer界面出现乱码+字体翻转
- 高仿腾讯QQ即时通讯IM项目
- 数据的聚合与分组运算
- jlink烧写linux内核,如何利用JLINK烧写U-boot到NAND Flash中
- 【html】svg标签
- UE4-PostProcessVolume(后期处理盒子)
- vue 数据更新却不render?
- MT-InSAR原理和发展_刘计洪
- FFT 多项式点表示与系数表示的快速fft转换