Insert into a Cyclic Sorted List

来源:互联网 发布:爱编程 编辑:程序博客网 时间:2024/05/28 22:09

Question:

Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the list.

 public static void insertCyclicList(Node head, int data) {Node node = new Node(data);if (head == null) {node.next = node;}Node current = head;Node prev = null;do {prev = current;current = current.next;if (data <= current.val && data >= prev.val) break;if (prev.val > current.val && (prev.val < data || current.val > data)) break;} while (head != current);prev.next = node;node.next = current;}

http://blog.csdn.net/beiyetengqing