Coding Interview Guide -- 向有序的环形单链表中插入新节点
生活随笔
收集整理的這篇文章主要介紹了
Coding Interview Guide -- 向有序的环形单链表中插入新节点
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【題目】
一個(gè)環(huán)形單鏈表從頭節(jié)點(diǎn)head開始不降序,同時(shí)由最后的節(jié)點(diǎn)指回頭節(jié)點(diǎn)。給定這樣一個(gè)環(huán)形單鏈表的頭節(jié)點(diǎn)head和一個(gè)整數(shù)num,請生成節(jié)點(diǎn)值為num的新節(jié)點(diǎn),并插入到這個(gè)環(huán)形鏈表中,保證調(diào)整后的鏈表依然有序
?
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 }?
?
來源:左程云老師《程序員代碼面試指南》
轉(zhuǎn)載于:https://www.cnblogs.com/latup/p/11018745.html
總結(jié)
以上是生活随笔為你收集整理的Coding Interview Guide -- 向有序的环形单链表中插入新节点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 快递100接口的调用过程
- 下一篇: 瑞文打不过JJ?