219. Insert Node in Sorted Linked List【Naive】
Insert a node in a sorted linked list.
Given list = 1->4->6->8 and val = 5.
Return 1->4->5->6->8.
解法一:
/**
* Definition of ListNode
* 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) {
ListNode * new_node = new ListNode(val);
if (head == NULL) {
return new_node;
} ListNode * dummy = new ListNode(-);
dummy->next = head;
head = dummy; while (head->next != NULL) {
if (val > head->next->val) {
head = head->next;
} else {
new_node->next = head->next;
head->next = new_node;
break;
}
} if (head->next == NULL) {
head->next = new_node;
} return dummy->next;
}
};
经典的构造dummy头节点问题
219. Insert Node in Sorted Linked List【Naive】的更多相关文章
- (链表) lintcode 219. Insert Node in Sorted Linked List
Description Insert a node in a sorted linked list. Example Example 1: Input: head = 1->4-> ...
- 237. Delete Node in a Linked List【easy】
237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...
- 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 ...
- 160. Intersection of Two Linked Lists【easy】
160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 《深入浅出node.js(朴灵)》【PDF】下载
<深入浅出node.js(朴灵)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062563 内容简介 <深入浅出Node. ...
- 234. Palindrome Linked List【easy】
234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...
- 114. Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为“只有右孩子节点”的链表(树)】
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
随机推荐
- 世界围棋人机大战、顶峰对决第二战:围棋世界冠军Lee Sedol(李世石,围棋职业九段)对战Google DeepMind AlphaGo围棋程序,AlphaGo再次胜出!
感觉在哔哩哔哩(bilibili)上看比赛直播比较好,一直可以看到比赛的直播画面,还能听到英文解说和中文主持人的解说.YouTube上是不错,但是一方面爬梯子比较卡,另一方面只能听到英文解说. 韩国著 ...
- Python操作dict时避免出现KeyError的几种方法
见原文:https://www.polarxiong.com/archives/Python-%E6%93%8D%E4%BD%9Cdict%E6%97%B6%E9%81%BF%E5%85%8D%E5% ...
- iOS:沙盒、偏好设置、归档、解归档
一.沙盒和应用程序包 •iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被称为沙盒 •iOS常用目录: –Bundle –Documents –Library/Ca ...
- 数学图形(1.39)TN constant curve
这是个类似巴黎铁塔的曲线. #http://www.mathcurve.com/courbes2d/tn/tn.shtml vertices = t = to (PI*0.999) a = s = s ...
- c# 滚动字幕的实现
在c#中其实滚动屏幕的实现很简单,只需要用到Graphics.DrawString方法. Graphics.DrawString (String s, Font font, Brush brush, ...
- 如何在CentOS 7中添加新磁盘而不用重启系统
导读 对大多数系统管理员来说扩充 Linux 服务器的磁盘空间是日常的工作之一.因此这篇文章会通过使用 Linux 命令,在 CentOS 7 系统上演示一些简单的操作步骤来扩充您的磁盘空间而不需要重 ...
- Asp.net 在网页编写C#代码示例-- 一个简单的web MsSql 命令执行环境
在给一个客户做的系统上,因为要对数据库进行查看,但之前都是用TeamView来连接到客户的服务器进行数据库操作的 但最近客户那边的TeamView好像更改过密码导致我无法正常连接,而巧了客户的网官因为 ...
- Android调用系统拍照裁剪和选图功能
最近项目中用到修改用户头像的功能,基本上都是模板代码,现在简单记录一下. 调用系统拍照 private fun openCamera() { //调用相机拍照 // 创建File对象,用于存储拍照后的 ...
- Android相机基础基于camera2API
前言 最近,在使用Android做一个照相机的开发.因为不能使用系统提供的相机应用,所以只能自己写一个.Android以前提供相机的api叫camera,不过在level 21被Google抛弃了.网 ...
- 霍炬:再谈百度:KPI、无人机,以及一个必须给父母看的案例
作者:霍炬. 原文链接:http://www.donews.com/idonews/article/8147.shtm 没想到我之前的一篇关于百度的文章引起了这么大的反馈. 非常多朋友称赞我写的好, ...