Question: JAVA - Write a function on your LinkedList class Node getParent(Node node)- given a Node in a LinkedList, finds and returns its parent. Return null

JAVA - Write a function on your LinkedList class Node getParent(Node node)- given a Node in a LinkedList, finds and returns its parent. Return null if the element exists but has no parent. Throw a NoSuchElementException if the element does not exist. Hint: Use two pointers that start at index 0 and index 1 of the list respectively, and iterate over the list with each one.

  1. For a LinkedList with values "A" -> "B" -> "C" -> "D", getParent(node "B") returns node "A"
  2. For a LinkedList with values "A" -> "B" -> "C" -> "D", getParent(node "A") returns null
  3. For a LinkedList with values "A" -> "B" -> "C" -> "D", getParent(node "E") throws NoSuchElementException

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!