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. add-strings

    https://leetcode.com/problems/add-strings/ package com.company; import java.util.LinkedList; import ...

  2. dedecms v5.5 final getwebshell exploit(datalistcp.class.php)

    测试方法: @Sebug.net   dis本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! <?php print_r(' +---------------------- ...

  3. go语言基础之不同目录

    1.不同目录 不同目录,包名不一样 调用不同包里面的函数,格式:包名,函数名() 调用别的包的函数,这个包函数名字如果是小写,无法让别人调用,要想别人能调用,必须首字母大写. 需要配置环境变量 临时配 ...

  4. C++对C的改进(1)

    本文地址:http://www.cnblogs.com/archimedes/p/cpp-change1.html,转载请注明源地址 新的初始化方法 C提供的初始化方法 int x = 1024; C ...

  5. Kettle中根据一个输入行派生出多个输出行

    依然在北京,早上停电了,整个人感觉对不好了,接下来就说一下在使用ETL工具kettle做数据校验的时候遇到的一些问题,一级解决方案. 1:数据校验效果图下图: 原始表数据(需要校验的表数据) 对上表数 ...

  6. Android provider中使用sqlite内存数据库

    sqlite是支持内存数据库的,在Android中,我们可以通过provider实现内存数据库操作.内存数据库的优点,访问速度快,但在连接关闭后,数据库自动消失(在android中的表现是,provi ...

  7. JQuery 之 跳出循环

    1.跳出each循环 return false 跳出循环 return true 进入下一个循环 2.跳出for循环 break;直接退出for这个循环.这个循环将不再被执行! continue;直接 ...

  8. 嵌入式 如何定位死循环或高CPU使用率(linux)

    如何定位死循环或高CPU使用率(linux)  确定是CPU过高 使用top观察是否存在CPU使用率过高现象 找出线程 对CPU使用率过高的进程的所有线程进行排序 ps H -e -o pid,tid ...

  9. 关于UbuntuMate的两个问题点:SSH问题处理与自启动项配置

    一.SSH连接报错问题 ssh到某台机器时候,存在如下报错: /usr/bin/xauth: timeout in locking authority file /home/sam/.Xauthori ...

  10. css3和html5的学习

    本文是此链接的源代码.http://www.imooc.com/learn/77 关于的html5的使用: transition----含义是:过渡的过程,能够对各种属性设置变化. 有5中过渡的形式: ...