题目如下:

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相关推荐

  1. 【LeetCode】86. 分隔链表

    86. 分隔链表 题解 这题不难,事先准备两个链表,遍历题目给的链表,将小于x和大于等于x的两类数据分别放到准备好的链表中,然后两个链表拼接即可: 注意点: 链表在创建增加的时候,游走的那个指针在当前 ...

  2. 【leetcode】416. Partition Equal Subset Sum

    题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...

  3. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题

    [LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...

  6. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  7. 【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 ...

  8. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  9. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

最新文章

  1. 用javascript实现一门编程语言-字符输入流
  2. java中对象的生存期_JSP中JavaBean的生命周期
  3. RKNN Toolkit使用教程
  4. SAP使用表空间传输异构系统迁移
  5. 【Python基础入门系列】第04天:Python 流程控制
  6. 聚类的概念和一般步骤
  7. 记一次MBR锁机病毒分析
  8. Python数据挖掘笔记 七 .PCA降维操作及subplot子图绘制
  9. 读书感受 之 《穷查理宝典》
  10. 前端工程师面试时自我介绍该如何做?
  11. 亚丁号云控之发布任务
  12. Visual SourceSafe Explorer界面出现乱码+字体翻转
  13. 高仿腾讯QQ即时通讯IM项目
  14. 数据的聚合与分组运算
  15. jlink烧写linux内核,如何利用JLINK烧写U-boot到NAND Flash中
  16. 【html】svg标签
  17. UE4-PostProcessVolume(后期处理盒子)
  18. vue 数据更新却不render?
  19. MT-InSAR原理和发展_刘计洪
  20. FFT 多项式点表示与系数表示的快速fft转换

热门文章

  1. 那些不能错过的Xcode插件
  2. 互联网大佬口口声声的人工智能,笑到最后的也许是马云的云计算
  3. 在Linux下安装RabbitMQ
  4. SQLServer2000 数据库恢复模型详解
  5. 腾讯云详解宕机故障:光纤挖断后的150秒
  6. 西藏监狱首次奏响《婚礼进行曲》
  7. Linux centos 安装VNC 服务过程
  8. Invalid indirect reference 0x28 in decodeIndirectR
  9. 通过代码获取Bitmap图片资源
  10. ×××视频下载:皮皮影视客户端功能使用