zoukankan      html  css  js  c++  java
  • 30 Day Challenge Day 6 | Hackerrank: Inserting a Node Into a Sorted Doubly Linked List

    题解

    很简单的题,要注意检查空节点。

    DoublyLinkedListNode* sortedInsert(DoublyLinkedListNode* head, int data) {
        if(!head) {
            head = new DoublyLinkedListNode(data);
            return head;
        }
    
        DoublyLinkedListNode* prev = nullptr;
        DoublyLinkedListNode* curr = head;
        while(curr && curr->data < data) {
            prev = curr;
            curr = curr->next;
        }
    
        DoublyLinkedListNode* node = new DoublyLinkedListNode(data);
    
        if(prev == nullptr) {
            node->next = head;
            return node;
        } else {
            prev->next = node;
            node->next = curr;
            return head;
        }
    }
    
  • 相关阅读:
    html5 File api 上传案例
    DOM操作
    箭头函数
    js 高级函数
    导入导出封装
    函数
    哲学/文学
    qtMd5 加密算法
    生活感悟
    C# 小技巧
  • 原文地址:https://www.cnblogs.com/casperwin/p/13636745.html
Copyright © 2011-2022 走看看