Coding Interview Guide -- 向有序的环形单链表中插入新节点
生活随笔
收集整理的這篇文章主要介紹了
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 }?
?
來源:左程云老師《程序員代碼面試指南》
轉載于:https://www.cnblogs.com/latup/p/11018745.html
總結
以上是生活随笔為你收集整理的Coding Interview Guide -- 向有序的环形单链表中插入新节点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快递100接口的调用过程
- 下一篇: js包装类型的装箱拆箱