LeetCode 86. 分隔链表(Partition List)
86. 分隔链表
86. Partition List
题目描述
给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。
你应当保留两个分区中每个节点的初始相对位置。
LeetCode86. Partition List中等
示例:
输出: 1->2->2->4->3->5
Java 实现
ListNode Class
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
@Override
public String toString() {
return val + "->" + next;
}
}
方法一
import java.util.LinkedList;
import java.util.Queue;
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
Queue<Integer> queue = new LinkedList<>();
while (head != null) {
if (head.val < x) {
tail.next = head;
tail = tail.next;
} else {
queue.add(head.val);
}
head = head.next;
}
while (!queue.isEmpty()) {
tail.next = new ListNode(queue.poll());
tail = tail.next;
}
return dummy.next;
}
}
方法二
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode dummy1 = new ListNode(0);
ListNode dummy2 = new ListNode(0);
ListNode curr1 = dummy1;
ListNode curr2 = dummy2;
while (head != null) {
if (head.val < x) {
curr1.next = head;
curr1 = curr1.next;
} else {
curr2.next = head;
curr2 = curr2.next;
}
head = head.next;
}
curr2.next = null; // very important!
curr1.next = dummy2.next;
return dummy1.next;
}
}
参考资料
LeetCode 86. 分隔链表(Partition List)的更多相关文章
- Leetcode 86.分隔链表
分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...
- LeetCode 86. 分隔链表(Partition List)
题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...
- LeetCode 86 ——分隔链表
1. 题目 2. 解答 从前向后遍历链表,将结点值小于 x 的结点放入到新链表 1 中,将结点值大于等于 x 的结点放入新链表 2 中.最后,将新链表 2 拼接在新链表 1 后面即可. /** * D ...
- 【LeetCode】86. 分隔链表
86. 分隔链表 知识点:链表: 题目描述 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个 ...
- Java实现 LeetCode 86 分割链表
86. 分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1 ...
- Java实现 LeetCode 725 分隔链表(暴力)
725. 分隔链表 给定一个头结点为 root 的链表, 编写一个函数以将链表分隔为 k 个连续的部分. 每部分的长度应该尽可能的相等: 任意两部分的长度差距不能超过 1,也就是说可能有些部分为 nu ...
- leetcode刷题-86分隔链表
题目 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4 ...
- [LeetCode题解]86. 分隔链表 | 三指针 + 虚拟头节点
解题思路 三指针,一个指向前半部分待插入位置,一个指向后半部分待插入位置,最后一个从前往后遍历 代码 /** * Definition for singly-linked list. * public ...
- [Swift]LeetCode86. 分隔链表 | Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- 创建django项目完整实例
虚拟环境搭配 安装和配置 安装虚拟环境的命令: 1)sudo pip install virtualenv #安装虚拟环境 2)sudo pip install virtualenvwrapper # ...
- Python2.7 报错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-4: ordinal not in range(128)
一. 错误原因(网上找的是这样说的,具体的我也不是很了解2.7版本的编码问题): 1.python默认使用ASCII处理字符流. 2.Unicode编码与ASCII编码的不兼容,Python脚本文件是 ...
- siameseNet网络以及信号分类识别应用
初学siameseNet网络,希望可以用于信号的识别分类应用.此文为不间断更新的笔记. siameseNet简介 全连接孪生网络(siamese network)是一种相似性度量方法,适用于类别数目多 ...
- Windows本机调试内部组件
将详细分析Windows调试的本机接口.希望读者对C和通用NT内核体系结构和语义有一些基本的了解.此外,这并不是介绍什么是调试或如何编写调试器.它可以作为经验丰富的调试器编写人员或好奇的安全专家的参考 ...
- pyqt5 + pyinstaller 制作爬虫小程序
环境:mac python3.7 pyqt5 pyinstaller ps: 主要是熟悉pyqt5, 加入了单选框 输入框 文本框 文件夹选择框及日历下拉框 效果图: pyqt5 主程序文件 # -* ...
- 2-STM32+W5500+GPRS(2G)基础篇-(W5500-学习说明)
https://www.cnblogs.com/yangfengwu/p/11220042.html 定版: 这一节先直接说明怎么把官方的源码应用在我做的这块开发板上 https://www.w550 ...
- 洛谷 P2136 拉近距离 题解
P2136 拉近距离 题目背景 我是源点,你是终点.我们之间有负权环. --小明 题目描述 在小明和小红的生活中,有N个关键的节点.有M个事件,记为一个三元组(Si,Ti,Wi),表示从节点Si有一个 ...
- js中forEach,for in,for of循环的用法详解
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...
- touchz,mkdir,vi的区别
touchz:创建空白文档 mkdir:创建一个目录 vi : 创建一个编辑状态的空文档,保存退出后创建成功.
- Hadoop 在启动或者停止的时候需要输入yes确认问题
启动或者停止hadoop的时候,信息如下: Stopping namenodes on [hadoop1 hadoop2] The authenticity of host 'hadoop2 (172 ...