(链表) lintcode 219. Insert Node in Sorted Linked List
Description
Insert a node in a sorted linked list.
Example
Example 1:
Input: head = 1->4->6->8->null, val = 5
Output: 1->4->5->6->8->null
Example 2:
Input: head = 1->null, val = 2
Output: 1->2->null
---------------------------------------------------------------------
就是在一个有序的链表中插入一个数。
C++代码:
注意有表头,表中间和表尾三个情况
/**
* Definition of singly-linked-list:
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/ class Solution {
public:
/**
* @param head: The head of linked list.
* @param val: An integer.
* @return: The head of new linked list.
*/
ListNode * insertNode(ListNode * head, int val) {
// write your code here
ListNode *cur = new ListNode(val);
ListNode *dummy = new ListNode(-);
dummy->next = head;
if(!head) return cur;
ListNode *p = dummy;
while(p->next){
if(p->next->val > val)
break;
else
p = p->next;
}
cur->next = p->next;
p->next = cur;
return dummy->next;
}
};
(链表) lintcode 219. Insert Node in Sorted Linked List的更多相关文章
- 219. Insert Node in Sorted Linked List【Naive】
Insert a node in a sorted linked list. Example Given list = 1->4->6->8 and val = 5. Return ...
- Insert Node in Sorted Linked List
Insert a node in a sorted linked list. Have you met this question in a real interview? Yes Example ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- [Java]LeetCode237. 删除链表中的节点 | Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- LeetCode 237. Delete Node in a Linked List 删除链表结点(只给定要删除的结点) C++/Java
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- LeetCode 237. Delete Node in a Linked List (在链表中删除一个点)
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- [LeetCode] 237. Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
随机推荐
- windows 服务中托管asp.net core
在windows 服务中托管asp.net core SDK 2.1.300 官方示例 1.添加运行标识符 xml <PropertyGroup> <TargetFramework& ...
- c/c++ 继承与多态 由子类向父类的转换规则
问题1:子类B可以有3种方式(public, protected, private)继承父类A,用哪种方式继承,用户代码才能把子类B的对象转换成父类A的对象呢? 只用当子类B以public方式继承父类 ...
- 南邮攻防训练平台逆向第四题WxyVM
下载文件elf文件,运行输入flag,用ida打开逆向算法: 不是很复杂,可以看出flag长度需要24,最终会和已给出dword_601060进行比较,一致则成功,那么现在只需要看上面的sub_400 ...
- js获取时间相关函数
js获取时间函数 var myDate = new Date; var year = myDate.getFullYear();//获取当前年 var yue = myDate.getMonth()+ ...
- SpringBoot+mybatis使用@Transactional无效
项目中新增过程中如果出现异常需要回滚, 在service实现方法中使用@Transactional注解失效 解决: 1, 在controller中使用try{}catch捕捉异常 2, 在servic ...
- 基于DataTables实现根据每个用户动态显示隐藏列,可排序
前言 在后台管理系统开发中,难免会出现列数太多的情况,这里提供一个解决方案:用户设置显示哪些列,每个用户互不影响,并且可以根据用户的习惯设置列的排序. 1.演示 2.html代码说明 3.java ...
- Ambari——大数据平台的搭建利器之进阶篇
前言 本文适合已经初步了解 Ambari 的读者.对 Ambari 的基础知识,以及 Ambari 的安装步骤还不清楚的读者,可以先阅读基础篇文章<Ambari——大数据平台的搭建利器>. ...
- VS 附加到进程 加载“附加进程”弹窗很慢
最近遇到一个问题,点击Ctrl + Alt + P 附加到进程的时候,弹出下图弹窗“附加到进程”很慢. 找了很多原因,后来发现,是因为少安装了一个插件,安装后,弹窗的耗时明显少了. 下载 Win ...
- UVA - 11478 - Halum(二分+差分约束系统)
Problem UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...
- day15-面向对象基础(二)
今天整理类的组合以及类的三大特性 1.类的组合 2.类的继承 3.类的封装 4.类的多态 开始今日份整理 1.类的组合 类与类之间,并不是独立的,很多的时候在正常使用的时候都是类与类之间互相调用,所以 ...