Write a java function that returns the maximum value in a circular doubly linked
ID: 3566620 • Letter: W
Question
Write a java function that returns the maximum value in a circular doubly linked list of integers, given the cursor c that points to a node in the list.Write a java function that returns the maximum value in a circular doubly linked list of integers, given the cursor c that points to a node in the list.
Explanation / Answer
class ListNode{ int item; ListNode next; } class RecursiveMax{ public static int max(ListNode node,int maxValue){ //is this the base case ? //if last node reached return the current maxValue if(node==null){ return maxValue; }else{ int v = node.item; if(v > maxValue){ return max(node.next,v); }else{ return max(node.next,maxValue); } } } public static void main(String[] args) { ListNode a = new ListNode(); a.item = 11; ListNode b = new ListNode(); b.item = 9; ListNode c = new ListNode(); c.item = 21; ListNode d = new ListNode(); d.item = 17; a.next = b; b.next = c; c.next = d; System.out.println("max value in linkedlist="+max(a,0)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.