(链表) lintcode 219. Insert Node in Sorted Linked List
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的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- C#LeetCode刷题之#237-删除链表中的节点(Delete Node in a Linked List)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3832 访问. 请编写一个函数,使其可以删除某个链表中给定的(非末 ...
随机推荐
- PGSQL-通过SQL语句来计算两个日期相差的天数
这是本人第一次写的~我在某次需求中遇到一个问题,如何在SQL语句中计算出两个日期的天数,然后用那个结果来进行数据的筛选呢?通过网上查阅了资料发现 date_part('day', cast(time1 ...
- iOS pthread
pthread 是属于 POSIX 多线程开发框架 创建线程的方法:pthread_create 参数含义: 1.指向线程代号的指针 2.线程的属性 3.指向函数的指针 4.传递给该函数的参数 返 ...
- Cs231n课堂内容记录-Lecture 6 神经网络训练
Lecture 6 Training Neural Networks 课堂笔记参见:https://zhuanlan.zhihu.com/p/22038289?refer=intelligentun ...
- 用人类社会工程学对C语言中的一些基本概念的剖析与理解
最近在学C语言程序设计时总是遇到一些概念理解上的不清晰与混乱的地方,在一次偶然间想到了以前看过的一部电影<我是谁,没有一个系统是安全的>,里面的主角用社会工程学的想法结合黑客知识化险为夷, ...
- centos7下kubernetes(15。kubernetes-外网访问service)
kubernetes提供了多种类型的service,默认是cluster IP ClusterIP cluster内部IP对外提供服务,只有cluster内的节点和pod可访问,这是默认的servic ...
- eclipse 创建springboot项目
eclipse创建springboot项目的三种方法: 引自:https://blog.csdn.net/mousede/article/details/81285693
- Redis数据持久化、数据备份、数据的故障恢复
1.redis持久化的意义----redis故障恢复 在实际的生产环境中,很可能会遇到redis突然挂掉的情况,比如redis的进程死掉了.电缆被施工队挖了(支付宝例子)等等,总之一定会遇到各种奇葩的 ...
- 检测web界面不能访问后重启
检测并重启脚本:checkAndRestart.sh #!/bin/bash nowpath=$(cd ")";pwd) source $nowpath/omcparam.prop ...
- 手动用tomcat启动war包,无法访问web项目
先说一下自己采的小坑,网上大多解答都是复制来复制去,不说重点在哪.我这里简单总结下访问路径问题 一.用idea打成war包,具体步骤如下图: 步骤:在项目配置选Artifacts新建Web Appli ...
- Linux--主从复制
一 . mysql+centos7 mariadb mariadb其实是跟mysql是一样的,只不过是在centos7上叫做mariadb, 主要是因为mysql被甲骨文公司收购后,可能会有闭源的风险 ...