Insert a node in a sorted linked list.

Example

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】的更多相关文章

  1. (链表) lintcode 219. Insert Node in Sorted Linked List

    Description   Insert a node in a sorted linked list.   Example Example 1: Input: head = 1->4-> ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  6. 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 ...

  7. 《深入浅出node.js(朴灵)》【PDF】下载

    <深入浅出node.js(朴灵)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062563 内容简介 <深入浅出Node. ...

  8. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  9. 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 ...

随机推荐

  1. [Todo] Redis里面队列的两种模式,以及抢红包在Redis中的实现

    两种队列模式: 一种是利用list的lpush/rpop等 另一种是redis自带的发布者/订阅者模式 http://www.cnblogs.com/alazalazalaz/p/5512258.ht ...

  2. C#正则表达式Regex类的介绍

    一.在C#中,要使用正则表达式类,请在源文件开头处添加以下语句: using System.Text.RegularExpressions; 二.RegEx类常用的方法 1.静态Match方法 使用静 ...

  3. Android -- Vibrator

    Vibrator                                                                                    public c ...

  4. Linux中基于hadoop安装hive(CentOS7+hadoop2.8.0+hive2.1.1)

    http://blog.csdn.net/pucao_cug/article/details/71773665

  5. Python 访问set

    访问set 由于set存储的是无序集合,所以我们没法通过索引来访问. 访问 set中的某个元素实际上就是判断一个元素是否在set中. 例如,存储了班里同学名字的set: >>> s ...

  6. Python 访问dict

    访问dict 1:可以if判断元素是否存在 2:dict.get('key')我们已经能创建一个dict,用于表示名字和成绩的对应关系:d = { 'Adam': 95, 'Lisa': 85, 'B ...

  7. js正则表达式test方法、exec方法与字符串search方法区别

    1.正则表达式test方法 test() 方法用于检测一个字符串是否匹配某个模式 返回值: 如果字符串 string 中含有与 RegExpObject 匹配的文本,则返回 true,否则返回 fal ...

  8. js 字符串indexof与search方法的区别

    1.indexof方法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法: 注意:有可选的参数(即设置开始的检索位置). 2.search方法 search() 方法用 ...

  9. WebApi2 知识点总结

    1.建议使用异步接口async Task<> public async Task<IHttpActionResult> Get() 如果返回的是IEnumerable请使用: ...

  10. Guava ---- Ordering排序工具

    使用Guava的排序工具类, 高速实现对象的单变量排序和多变量排序, 让你的开发效率爆炸... import com.google.common.collect.Lists; import com.g ...