The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
| public class SinglyLinkedList<E> implements List<E>{ Â Â private static class Node<E>{ Â Â Â private E value; Â Â Â private Node<E> next; Â Â Â private Node(E value, Node<E> next){ Â Â Â Â Â this.value = value; Â Â Â Â Â this.next = next; Â Â Â } Â Â } Â Â private Node<E> head; //class continues after that |
| public boolean mystery1(E o){ Â Â if (o == null){ Â Â Â Â throw new NullPointerException("null"); Â Â } Â Â if (head == null){ Â Â Â Â return false; Â Â } Â Â boolean result = mystery1(head, o); Â Â if (head.value.equals(o)){ Â Â Â Â head = head.next; Â Â Â Â return true; Â Â } else{ Â Â Â Â return result; Â Â } } private boolean mystery1(Node<E> p, E o){ Â Â if (p.next == null){ Â Â Â Â return false; Â Â } Â Â boolean result = mystery1(p.next, o); Â Â if (p.next.value.equals(o)){ Â Â Â Â p.next = p.next.next; Â Â Â Â return true; Â Â } else{ Â Â Â Â return result; Â Â } } |
|
SinglyLinkedList<String> list = new SinglyLinkedList<String>(); Â list.add("A"); list.add("B"); list.add("C"); list.add("A"); list.add("B"); list.add("C"); list.add("A"); list.add("B"); list.add("C"); System.out.println(list); System.out.println(list.mystery1("A")); System.out.println(list); System.out.println(list.mystery1("C")); System.out.println(list); System.out.println(list.mystery1("A")); System.out.println(list); |
Solution:
[A, B, C, A, B, C, A, B, C]
true
[B, C, B, C, B, C]
true
[B, B, B]
false
[B, B, B]
Question:
Can you explain step by step how do you get the answer when the function mystery1 is called? and how does recursion work in this program