首页 > 其他 > 详细

"Coding Interview Guide" -- 向有序的环形单链表中插入新节点

时间:2019-06-13 19:03:35      阅读:120      评论:0      收藏:0      [点我收藏+]

题目

  一个环形单链表从头节点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     }

 

 

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

"Coding Interview Guide" -- 向有序的环形单链表中插入新节点

原文:https://www.cnblogs.com/latup/p/11018745.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!