Insert into a Cyclic Sorted List

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

你可能感兴趣的:(list,function,null,insert,Go)