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 ...
随机推荐
- MySQL中SYSDATE()和NOW()函数的区别和联系
MySQL中有5个函数需要计算当前时间的值: NOW.返回时间,格式如:2012-09-23 06:48:28 CURDATE,返回时间的日期,格式如:2012-09-23 CURTIME,返回时间, ...
- Android程序调试
1. 使用Log.d方法输出Debug日志信息. Log.d方法用来输出DEBUG故障日志信息,该方法有两种重载形式,其中开发人员经常用到的重载形式语法如下: public static int d( ...
- 如何查看ESXi的网卡的MAC地址?
直接上图 图一, 物理网卡 图二, vmkernel虚拟网卡 参考资料 ============================ How To Determine Vmkernel Inter ...
- jquery easyui里datagrid用法记录
1.删除行方法(deleteRow) $(); //1代表选中的行索引 2.删除多行数据 var rows = $('#ruleManagementTable').datagrid("get ...
- ECharts学习总结(三):ECharts图表对象的初始化(init)详解以及注意事项
一.相关js文件的引入 这里我们采用标签式引入文件的方式,我们引入两个js文件,一个是esl.js文件和一个echarts.js. <script src="js/esl.js&quo ...
- 值得阅读的C语言开源项目代码
本文地址:http://www.cnblogs.com/archimedes/p/c-opensource-project.html,转载请注明源地址. 本篇文章主要总结一些C开源项目,有些是很著名的 ...
- JVisualVM简介与内存泄漏实战分析
JVisualVM简介与内存泄漏实战分析 学习了:https://blog.csdn.net/kl28978113/article/details/53817827
- FileItem 出现部分中文乱码解决办法
首先要进行两处的修改: 第一:如果你使用了上传文件的包, 如:ServletFileUpload sfu = new ServletFileUpload(factory); sfu.setHeader ...
- UNIX网络编程读书笔记:简介
认知套接口编程接口 理解原始套接口(raw socket)的概念 值得注意的是,客户和服务器是典型的用户进程,而TCP和IP协议则通常是系统内核协议栈的一部分. 上图中在TCP和UDP之间留有间隙 ...
- 解决Android 5.1系统以上通知状态栏小图标仅仅显示白色问题
看上图,想必大家都有遇到过吧.近期俺也遇到了,找到了解决方法,如今分享下也做个记录哈. 问题发生的规则是Android5.1或者以上的手机系统使用了非常多的颜色的通知图标,就会出现,怎么解决呢,非常e ...