Given the two classes below, provide the correct implementation for the deleteSp
ID: 3737316 • Letter: G
Question
Given the two classes below, provide the correct implementation for the deleteSpecificInteger method that allows you to delete a specific integer from the linked list. If there are multiple occurrences of the same integer, then remove all occurrences of that integer.
class Node {
Node next;
Integer item;
public Node(Integer val) {
item = val;
next = null;
}
public Node(Integer val, Node n) {
item = val;
next = n;
}
}
public class LinkedList {
Node head;
public LinkedList(Integer val) {
head = new Node(val);
}
public void append(Integer val) {
Node tmpNode = head;
while (tmpNode.next != null) {
tmpNode = tmpNode.next;
}
tmpNode.next = new Node(val);
}
public void deleteSpecificInteger(Integer x){
}
}
Explanation / Answer
//LinkedList .java public class LinkedList { Node head; public LinkedList(Integer val) { head = new Node(val); } public void append(Integer val) { Node tmpNode = head; while (tmpNode.next != null) { tmpNode = tmpNode.next; } tmpNode.next = new Node(val); } public void deleteSpecificInteger(Integer x){ Node tmpNode = head; while (tmpNode.item != x.intValue()) { tmpNode = tmpNode.next; } if(tmpNode != null && tmpNode.next!=null){ tmpNode.next = tmpNode.next.next; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.