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

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

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

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

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

  5. [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 ...

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

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

  8. [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 ...

  9. C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...

随机推荐

  1. 虚拟机安装macos 分辨率不正常修改不了,不能全屏如何解决

    1.苹果在OSX 10.11之后启动了一个System Integrity Protection (SIP),这个保护系统防止/library/preferences/systemconfigurat ...

  2. REST风格架构

    一说到rest 大家都耳熟能详,很多人的第一反应就是其是前后端请求后台的一种通信方式,甚至有些人将REST 和RPC 混为一谈,认为两者都是基于HTTP类似的东西.实际上很少人能叙述REST 所提出的 ...

  3. markdown小知识总结

    字体.字号.颜色 但如果我们想修改文字大小/颜色/字体,就要用font标签,代码如下: 宋体大小为2的字 color代表字体颜色(要用16进制颜色值),size代表文字大小,face代表字体 效果展示 ...

  4. centos7下kubernetes(12。kubernetes-service)

    Service:定义了一个服务得访问入口地址,前端的应用通过这个入口地址访问其背后得一组由pod副本组成的集群实例: service与后端的pod副本集群之间则是通过label selector来实现 ...

  5. Strem_01

    import 'package:flutter/material.dart';import 'dart:async';import 'dart:ui'; void main()=>runApp( ...

  6. UI Automator 常用 API 整理

    主要类: import android.support.test.uiautomator.UiDevice; 作用:设备封装类,测试过程中获取设备信息和设备交互. import android.sup ...

  7. Python爬虫 爬取百合网的女人们和男人们

    学Python也有段时间了,目前学到了Python的类.个人感觉Python的类不应称之为类,而应称之为数据类型,只是数据类型而已!只是数据类型而已!只是数据类型而已!重要的事情说三篇. 据书上说一个 ...

  8. C#中的Finalize,Dispose,SuppressFinalize(转载)

    MSDN建议按照下面的模式实现IDisposable接口: public class Foo : IDisposable { public void Dispose() { Dispose(true) ...

  9. [Oracle维护工程师手记]一次升级后运行变慢的分析

    客户报告,当他从 Oracle 11.1.0.7 ,迁移到云环境,并且升级到12.1.0.2.运行客户的应用程序测试,发现比以前更慢了. 从AWR report 的"Top 10 Foreg ...

  10. 06-JavaScript的流控制语句

    06-JavaScript的流控制语句 JavaScript的流控制语句主要分为三大类: 顺序控制:因为JS是一门解释性语言,所以从上至下按顺序依次执行 分支控制:主要分为if条件语句和swith开关 ...