首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
分隔链表
[编程题]分隔链表
热度指数:164
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有小于 x 的节点都出现在大于或等于 x 的节点之前。
你应当保留两个分区中每个节点的初始相对位置。
数据范围:节点的数量满足
,节点上的值和输入的 x 满足
示例1
输入
{1,4,3,2,5,2},3
输出
{1,2,2,4,3,5}
示例2
输入
{2,1},2
输出
{1,2}
说明:本题目包含复杂数据结构ListNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(2)
分享
纠错
提交结果有问题?
3个回答
0篇题解
添加回答
1
cmls
NC23
划分链表 同款题目?
发表于 2022-02-09 10:59:21
回复(0)
0
牛客830289240号
typedef struct ListNode ListNode;
struct ListNode* partition(struct ListNode* head, int x){
if(head == NULL) // 如果传入的链表是空链表
return NULL;
ListNode* lessHead = (ListNode*)malloc(sizeof(ListNode)); // 哨兵位
ListNode* lessTail = lessHead;
lessTail->next = NULL;
ListNode* greaterHead = (ListNode*)malloc(sizeof(ListNode)); // 哨兵位
ListNode* greaterTail = greaterHead;
greaterTail->next = NULL;
ListNode* cur = head;
while(cur)
{
printf("cur->val=%d\n", cur->val);
if(cur->val < x) // 找小于x的节点
{
lessTail->next = cur;
lessTail = lessTail->next;
printf("lessTail->val=%d\n", lessTail->val);
}
else // 找大于或等于x的节点
{
greaterTail->next = cur;
greaterTail = greaterTail->next;
printf("greaterTail->val=%d\n", greaterTail->val);
}
cur = cur->next;
}
lessTail->next = greaterHead->next; // 对两个链表进行拼接
greaterTail->next = NULL; // 置空,防止新链表出现环
ListNode* list = lessHead->next; // list是新链表真正意义上的头节点
free(greaterHead); // 释放哨兵位
free(lessHead); // 释放哨兵位
return list;
}
发表于 2023-02-18 22:38:24
回复(1)
0
Python
牛客782612709号
'''双指针插入链表节点,一共分两步'''
class Solution:
def partition(self , head: ListNode, x: int) -> ListNode:
dummy,p1 = ListNode(0),head
dummy.next = head
p1pre = dummy
'''step1 找到目标分界节点,及其前一个节点'''
while p1!=None and p1.val<x:
p1pre = p1
p1 = p1.next
'''step 2 从目标分界节点 p1 往后遍历检查,值<x的节点就往 prep1,p1中 间***r /> p2pre,p2 = p1pre,p1
while p2!=None:
if p2.val<x:
p2pre.next = p2.next #断尾
p2.next = p1 #插到前边
p1pre.next = p2
p1pre = p2 #指针重置给下一轮
p2 = p2pre.next
else:
p2pre = p2
p2 = p2.next
return dummy.next
发表于 2022-02-09 19:02:52
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
链表
双指针
难度:
3条回答
2收藏
1687浏览
热门推荐
通过挑战的用户
牛客83028...
2023-02-20 14:23:08
我也很努力的在学了
2022-10-19 16:24:05
菩提树下__
2022-10-09 14:46:15
男嘻嘻
2022-06-07 15:02:07
风逝无迹
2022-05-14 16:52:08
相关试题
和为S的两个数字
数组
数学
双指针
评论
(1508)
来自
“一战通offer”互联...
最小面积子矩阵
动态规划
双指针
前缀和
评论
(44)
神奇的数字
排序
双指针
评论
(46)
“乔布斯不做调查,张小龙不看数据。...
用户研究
评论
(1)
如何检验聚类分析结果
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ public ListNode partition (ListNode head, int x) { // write code here } }
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ ListNode* partition(ListNode* head, int x) { // write code here } };
#coding:utf-8 # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @param x int整型 # @return ListNode类 # class Solution: def partition(self , head , x ): # write code here
using System; using System.Collections.Generic; /* public class ListNode { public int val; public ListNode next; public ListNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ public ListNode partition (ListNode head, int x) { // write code here } }
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ function partition( head , x ) { // write code here } module.exports = { partition : partition };
val = $x; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ function partition( $head , $x ) { // write code here }
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @param x int整型 # @return ListNode类 # class Solution: def partition(self , head: ListNode, x: int) -> ListNode: # write code here
package main import "fmt" import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ func partition( head *ListNode , x int ) *ListNode { // write code here }
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ struct ListNode* partition(struct ListNode* head, int x ) { // write code here }
# class ListNode # attr_accessor :val, :next # # def initialize(val = 0, _next = nil) # @val, @next = val, _next # end # end # # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @param x int整型 # @return ListNode类 # class Solution def partition(head, x) # write code here end end
/** * class ListNode(var val: Int) { * var next: ListNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ def partition(head: ListNode,x: Int): ListNode = { // write code here } }
/** * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ fun partition(head: ListNode?,x: Int): ListNode? { // write code here } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ public ListNode partition (ListNode head, int x) { // write code here } }
/*class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ export function partition(head: ListNode, x: number): ListNode { // write code here }
/** * public class ListNode { * public var val: Int * public var next: ListNode? * public init(_ val: Int = 0, _ next: ListNode? = nil) { * self.val = val * self.next = next * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ func partition ( _ head: ListNode?, _ x: Int) -> ListNode? { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct ListNode { * pub val: i32, * pub next: Option
> * } * * impl ListNode { * #[inline] * fn new(val: i32) -> Self { * ListNode { * val: val, * next: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ pub fn partition(&self, head: Option
>, x: i32) -> Option
> { // write code here } }
{1,4,3,2,5,2},3
{1,2,2,4,3,5}
{2,1},2
{1,2}