Insert a node in a sorted linked list.

Have you met this question in a real interview? 
Yes
Example

Given list = 1->4->6->8 and val = 5.

Return 1->4->5->6->8.

分析


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param head: The head of linked list.
     * @param val: an integer
     * @return: The head of new linked list
     */
    public ListNode insertNode(ListNode head, int val) { 
        // Write your code here
        ListNode dummy = new ListNode(0);
        ListNode node = new ListNode(val);
        dummy.next = head;
         
        ListNode pos = dummy;
        ListNode next = head;
         
        while(pos.next != null && pos.next.val < val){
            pos = pos.next;
            next = next.next;
        }
         
        node.next = next;
        pos.next = node;
         
        return dummy.next;
    }  
}

Insert Node in Sorted Linked List的更多相关文章

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

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

  5. 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. 【4_237】Delete Node in a Linked List

    Delete Node in a Linked List Total Accepted: 48121 Total Submissions: 109297 Difficulty: Easy Write ...

  7. Leetcode-237 Delete Node in a Linked List

    #237.    Delete Node in a Linked List Write a function to delete a node (except the tail) in a singl ...

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

  9. (easy)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 ...

随机推荐

  1. Linux checksum flag in kernel

    net_device->feature | NETIF_F_NO_CSUM: No need to use L4 checksum, it used for loopback device. | ...

  2. 002 -- MySQL的逻辑架构

                                            msql的逻辑架构图 第一层:主要功能是连接处理.授权认证.安全等.相当于JavaEE中的常说的Web层 第二层:包含了 ...

  3. sqli-labs学习笔记 DAY1

    DAY 1 准备工作 安装phpstudy 安装配置sqli-labs 学习笔记 SQL语句的注释:–, # +在URL经过编码后会编码为空格 SQL语句的查询语句:SELECT column_nam ...

  4. 微软职位内部推荐-Senior Software Engineer II-Search

    微软近期Open的职位: Do you want to work on a fast-cycle, high visibility, hardcore search team with ambitio ...

  5. [笔记] centos6.6编译安装httpd2.4.10

    系统安装包是CentOS-6.6-x86_64-minimal.iso 查看一下uname信息 [root@localhost ~]# uname -a Linux localhost.localdo ...

  6. java不用任何已有方法完全自写的去重法

    package aa; class InsertSort{ private long[] a; private int nElems; //构造方法 public InsertSort(int max ...

  7. Scrum立会报告+燃尽图(06)选题

    此作业要求参见:[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2195] 一.小组介绍 组长:王一可 组员:范靖旋,王硕,赵佳璐,范洪达 ...

  8. PSP DAILY软件功能说明书

    PSP DAILY软件功能说明书 一.开发背景 你在完成了一周的软件工程作业后,需要提交一个PSP图表,里面有4项,如下所示: 1.本周PSP表格,包含每项任务的开始.中断.结束.最终时间,格式如下: ...

  9. c# apache服务器请求得到数据(初级)

    1.代码: string data = new WebClient().DownloadString("http://localhost:81/123.txt");

  10. 404 Note Found -(课堂实战)- 项目UML设计(团队)

    目录 团队信息 分工选择 课上分工 课下分工 ToDolist alpha版本要做的事情 燃尽图 UML 用例图 状态图 活动图 类图 部署图 实例图 对象图 时序图 包图 通信图 贡献分评定 课上贡 ...