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 ...
随机推荐
- Spring Bean 注入 2 注解篇
1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...
- poj2186 Popular Cows 题解——S.B.S.
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29642 Accepted: 11996 De ...
- iOS:shareSDK第三方登录
shareSDK第三方登录跟分享差不多,比较简单,前面已有介绍.这里简单写一下第三方登录吧. 1.首先:我用到了QQ.微信.新浪这三个平台的登录,需要到它们各自的开发者平台注册开发者账号,这是我的QQ ...
- Web开发中的6个坏习惯
在 Usersnap,我们在能很好的组织网站开发有超过20(总和)年的经验.我们认为这些过去的经验能让我们很好的分辨出什么是好.坏和丑陋的网站开发.如今我们不想把注意力放在消极的部分,但就这一次,我们 ...
- PHP实现双向链表
看了很久数据结构但是没有怎么用过,在网上看到了关于PHP的数据结构,学习了一下,与大家一起分享一下.上一次分享了链表,这次来补充说一下双向链表. 简短不割 ...
- [JS Compose] 3. Use chain for composable error handling with nested Eithers (flatMap)
We refactor a function that uses try/catch to a single composed expression using Either. We then int ...
- org.hibernate.MappingException: An association from the table order_intem_inf refers to a unmapped
执行一个HIbernate的演示样例时出现例如以下错误信息 Exception in thread "main" java.lang.ExceptionInInitializerE ...
- <The Art of Readable Code> 笔记一
第1章 代码应易理解 (Code should be easy to understand) 基本原则:好的代码,能够减少 “别人” 理解它的时间. “别人” 不仅指的是 “其它人”,也可能是 “以 ...
- 算法笔记_044:表达式计算求值(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 输入一个只包含加减乖除和括号的合法表达式,求表达式的值.其中除表示整除. 输入格式 输入一行,包含一个表达式. 输出格式 输出这个表达式的 ...
- cookie 封装
1.代码 ;(function (factory) { var registeredInModuleLoader; if (typeof define === 'function' && ...