1. 题目

2. 解答

从前向后遍历链表,将结点值小于 x 的结点放入到新链表 1 中,将结点值大于等于 x 的结点放入新链表 2 中。最后,将新链表 2 拼接在新链表 1 后面即可。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) { if (head == NULL || head->next == NULL) return head; ListNode *new_head1 = new ListNode(0); // 新建哨兵结点方便操作
ListNode *temp1 = new_head1; ListNode *new_head2 = new ListNode(0); // 新建哨兵结点方便操作
ListNode *temp2 = new_head2; while (head)
{
if (head->val < x) // 小于 x 的结点放入新链表 1
{
temp1->next = head;
temp1 = head;
}
else // 大于等于 x 的结点放入新链表 2
{
temp2->next = head;
temp2 = head;
} head = head->next;
} temp1->next = new_head2->next;
temp2->next = NULL; return new_head1->next;
}
};

获取更多精彩,请关注「seniusen」!

LeetCode 86 ——分隔链表的更多相关文章

  1. LeetCode 86. 分隔链表(Partition List)

    86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...

  2. Leetcode 86.分隔链表

    分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...

  3. LeetCode 86. 分隔链表(Partition List)

    题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1-> ...

  4. 【LeetCode】86. 分隔链表

    86. 分隔链表 知识点:链表: 题目描述 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个 ...

  5. Java实现 LeetCode 86 分割链表

    86. 分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1 ...

  6. Java实现 LeetCode 725 分隔链表(暴力)

    725. 分隔链表 给定一个头结点为 root 的链表, 编写一个函数以将链表分隔为 k 个连续的部分. 每部分的长度应该尽可能的相等: 任意两部分的长度差距不能超过 1,也就是说可能有些部分为 nu ...

  7. leetcode刷题-86分隔链表

    题目 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4 ...

  8. [LeetCode题解]86. 分隔链表 | 三指针 + 虚拟头节点

    解题思路 三指针,一个指向前半部分待插入位置,一个指向后半部分待插入位置,最后一个从前往后遍历 代码 /** * Definition for singly-linked list. * public ...

  9. C#LeetCode刷题-链表

    链表篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 19 删除链表的倒数第N个节点   29.4% 中等 21 合并两个有序链表 C#LeetCode刷题之#21-合并两个有序链 ...

随机推荐

  1. c++ 单继承派生类的构造函数

    1.派生类的构造函数: #include <iostream> #include<string> using namespace std; class Student//声明基 ...

  2. react-native环境配置入坑指南.

    官方入门教程:https://reactnative.cn/docs/0.51/getting-started.html http://services.gradle.org/distribution ...

  3. repo配置与连接

    repo是远程访问android源码的工具,和git一起使用. repo的远程安装经常被屏蔽,你懂得. sudo apt-get  install  curl  244  sudo apt-get - ...

  4. Long数组转String数组

    public static String[] longToString(Long longArray[]) { if (longArray == null || longArray.length &l ...

  5. jzoj5195. 【NOIP2017提高组模拟7.3】A(递推,打表)

    Description

  6. C/C++远程开机

    // 2C:4D:54:ED:08:F0 #include <stdio.h> #include <Windows.h> #include <winsock.h> ...

  7. mysql修改登录密码三种方式

    一.用SET PASSWORD命令 首先登录MySQL,使用mysql自带的那个客户端连接上mysql.  格式:mysql> set password for 用户名@localhost = ...

  8. python元组操作

    元组:(tuple)元素不可被修改,不能被增加或者删除 一般写元组的时候,建议在最后加上一个逗号 可以索引取值    可以切片取值 元组一级元素不可被修改,但是二级及以后可以被修改 count() 获 ...

  9. Plugin was not installed: Cannot download 'https://plugins.jetbrains.com/pluginManager''

    在Android studio中安装插件的时候,提示了类似这种的错误,解决这个问题有以下几步 1.打开Configure->Settings 2.System Settings->Upda ...

  10. 揭开js之constructor属性的神秘面纱

    揭开 constructor 在 Javascript 语言中,constructor 属性是专门为 function 而设计的,它存在于每一个 function 的prototype 属性中.这个 ...