(链表) 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(-1);
        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;
    }
};


原文地址:https://www.cnblogs.com/Weixu-Liu/p/10758074.html

时间: 2024-10-11 01:23:27

(链表) lintcode 219. Insert Node in Sorted Linked List的相关文章

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree Example Given binary search tree as follow, after Insert node 6, the tree should be: 2 2 / \ / 1 4 --> 1 4 / / \ 3 3 6 Challenge Can you do it without recursion? /** * Definition of TreeNode: * public class TreeNod

[LeetCode]79. 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 that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 ->

leetcode_237题——Delete Node in a Linked List(链表)

Delete Node in a Linked List Total Accepted: 6113 Total Submissions: 12798My Submissions Question Solution Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2

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 singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked

LeetCode 237 Delete Node in a Linked List(在链表中删除节点)

翻译 给定一个访问节点的路径,写一个函数去删除在一个单向链表中除尾部以外的节点. 假设这个链表是1 -> 2 -> 3 -> 4,并且你被给予了第3个值为3的节点,那么在调用你的函数之后这个链表应该变为1 -> 2 -> 4. 原文 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the l

LeetCode237_Delete Node in a Linked List(删除链表中的节点) Java题解

题目: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -

[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 that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 ->

[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 to that node.EXAMPLEInput: the node c from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a- >

237.单链表移除节点 Delete Node in a Linked List

Question Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 ->