zoukankan      html  css  js  c++  java
  • "Coding Interview Guide" -- 向有序的环形单链表中插入新节点

    题目

      一个环形单链表从头节点head开始不降序,同时由最后的节点指回头节点。给定这样一个环形单链表的头节点head和一个整数num,请生成节点值为num的新节点,并插入到这个环形链表中,保证调整后的链表依然有序

     1     public Node insertNum(Node head, int num)
     2     {
     3         Node node = new Node(num);
     4         if(head == null)
     5         {
     6             node.next = node;
     7             return node;
     8         }
     9 
    10         Node pre = head;
    11         Node cur = head.next;
    12         while(cur != head)
    13         {
    14             if(pre.value <= num && cur.value >= num)
    15             {
    16                 break;
    17             }
    18             pre = cur;
    19             cur = cur.next;
    20         }
    21         pre.next = node;
    22         node.next = cur;
    23         return head.value < num ? head : node;    
    24     }

    来源:左程云老师《程序员代码面试指南》

  • 相关阅读:
    (DP)codeforces
    (树状数组)POJ
    (树状数组)POJ
    (二维树状数组)POJ
    (模拟)HDU
    (暴力+深搜)POJ
    (判连通+暴力)UVA
    (深搜)UVA
    (暴力+各种算法)hihoCoder
    (尺取法)HDU
  • 原文地址:https://www.cnblogs.com/OoycyoO/p/11018745.html
Copyright © 2011-2022 走看看