首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
对链表进行插入排序
[编程题]对链表进行插入排序
热度指数:1646
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
对链表进行插入排序,
从链表第一个元素开始可以视为部分已排序,每次操作从链表中移除一个元素,然后原地将移除的元素插入到已排好序的部分。
数据范围:链表长度满足
,链表中每个元素满足
例如输入{2,4,1}时,对应的返回值为{1,2,4},转换过程如下图所示:
示例1
输入
{1,2,3}
输出
{1,2,3}
示例2
输入
{2,4,1}
输出
{1,2,4}
说明:本题目包含复杂数据结构ListNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(31)
分享
纠错
提交结果有问题?
15个回答
10篇题解
开通博客
用户抉择
发表于 2022-07-10 22:57:41
class Solution { public: ListNode* insertionSortList(ListNode* head) {
展开全文
ZONE.WANG
发表于 2021-12-10 16:47:12
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * pub
展开全文
姐姐的遮阳伞
发表于 2022-04-04 21:21:58
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; *
展开全文
fred-coder
发表于 2021-12-23 01:00:17
插入排序思想,当前值 <= val 时继续迭代,直到遇到较大值,由于链表结构,需要重置 next 节点,若是数组要进行移位操作 # class ListNode: # def __init__(self, x): # self.val = x # sel
展开全文
太阳hxy
发表于 2023-07-15 10:21:09
对链表进行插入排序 思路: 1.只要还没有走到链表的倒数第二个节点,就表明插入排序还没有完成,就继续进行插入排序 2.对于每次插入排序,都需要遍历整个链表,只要是保持升序的就跳过,直到找到第一个打破升序的节点 3.先判断一下如果发现p.next为null,就表明原先已有的链表就是有序的,就可以直
展开全文
a_fighter_named_rudy
发表于 2022-10-12 23:41:45
/** * struct ListNode { * int val; * struct ListNode *next;
展开全文
River_68
发表于 2022-04-04 21:17:15
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 *
展开全文
AsherChi
发表于 2022-08-21 14:50:11
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : v
展开全文
苦行潜修者
发表于 2024-04-20 18:30:05
using System; using System.Collections.Generic; /* public class ListNode { public int val; public ListNode next; public ListNode (int x)
展开全文
ice_cheers
发表于 2022-04-11 20:48:45
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: /*
展开全文
问题信息
链表
排序
难度:
15条回答
31收藏
1769浏览
热门推荐
通过挑战的用户
查看代码
外骨骼
2022-09-16 01:52:21
摆烂牛牛阿西吧
2022-09-15 19:10:53
ৡ晨ꦿ᭄࿐
2022-09-14 15:15:44
Asphyxiay
2022-09-14 01:45:16
Endinggg
2022-09-12 15:41:45
相关试题
分页系统的逻辑地址结构是一维的,分...
操作系统
评论
(1)
未来工作城市的选择是怎样的?
通用能力
评论
(1)
你说在销售运营这个岗位上会涉及到一...
评论
(1)
相关性分析有哪些?
评论
(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类 * @return ListNode类 */ public ListNode insertionSortList (ListNode head) { // write code here } }
/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ ListNode* insertionSortList(ListNode* head) { // write code here } };
#coding:utf-8 # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution: def insertionSortList(self , head ): # 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类 * @return ListNode类 */ public ListNode insertionSortList (ListNode head) { // write code here } }
/* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ function insertionSortList( head ) { // write code here } module.exports = { insertionSortList : insertionSortList };
val = $x; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ function insertionSortList( $head ) { // write code here }
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution: def insertionSortList(self , head: ListNode) -> ListNode: # write code here
package main import "fmt" import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ func insertionSortList( head *ListNode ) *ListNode { // write code here }
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ struct ListNode* insertionSortList(struct ListNode* head ) { // write code here }
# class ListNode # attr_accessor :val, :next # # def initialize(val = 0, _next = nil) # @val, @next = val, _next # end # end # # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution def insertionSortList(head) # write code here end end
/** * class ListNode(var val: Int) { * var next: ListNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ def insertionSortList(head: ListNode): ListNode = { // write code here } }
/** * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return ListNode类 */ fun insertionSortList(head: ListNode?): 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类 * @return ListNode类 */ public ListNode insertionSortList (ListNode head) { // 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类 * @return ListNode类 */ export function insertionSortList(head: ListNode): 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类 * @return ListNode类 */ func insertionSortList ( _ head: ListNode?) -> 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类 * @return ListNode类 */ pub fn insertionSortList(&self, head: Option
>) -> Option
> { // write code here } }
{1,2,3}
{1,2,3}
{2,4,1}
{1,2,4}