(链表) 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 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
随机推荐
- 定时删除所有文件夹下的_desktop.ini文件
写个批处理,删除对应的文件,命名为DELDesktopIni.bat,存于D盘根目录 @echo off :delini for %%a in ( C: D: E: ) DO ( del /f/s/a ...
- jQuery中toggle与slideToggle以及fadeToggle之间的不同
toggle()方法: 定义和用法 切换元素的可见状态.如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素. 语法: $(selector).toggle(speed,callback ...
- Linux-基础学习(一)-基本命令
开始今日份整理 1.Linux的文件目录操作 1.1 ls 简述:ls是list的缩写,用于列出指定目录或文件 常用的选项 1 -a:显示所有档案及目录(ls内定将档案名或目录名称为“.”的视为隐藏, ...
- pybind11 安装
Prerequisites: $ sudo apt-get install python-dev (or python3-dev) $ sudo apt-get install cmake $ su ...
- postman的使用大全
转载 https://blog.csdn.net/fxbin123/article/details/80428216
- UIImagePickerController - 官方文档说明
使用UIImagePickerController对象的步骤: 1)验证设备是否能从目标源获取内容,通过调用 + (BOOL)isSourceTypeAvailable:(UIImagePickerC ...
- aelf帮助C#工程师10分钟零门槛搭建DAPP&私有链开发环境
aelf是一个可扩展的去中心化云计算区块链平台,支持高性能合约并行执行.原生多链数据交互.存储使用高性能分布式数据库. aelf整个系统可以在windows.osx及linux运行,团队在osx环境下 ...
- H5 俄罗斯方块Demo
链接:http://pan.baidu.com/s/1hrDM0T2
- 新Chrome浏览器不支持html5的问题
window.applicationCache事件,最新chrome浏览器已经不能判断是否支持html5: 之前,在IE和Google中 为ApplicationCache对象,而在FF中为 Offl ...
- JS 数组去重的几种方式
JS 常见的几种数组去重方法 一.最简单方法(indexOf 方法) 实现思路:新建一个数组,遍历要去重的数组,当值不在新数组的时候(indexOf 为 -1)就加入该新数组中: function u ...