Question

Java Given the array, which has been converted to a linked list, I need to invoke...

Java

Given the array, which has been converted to a linked list, I need to invoke
a remove method to remove a number from the list. The output for the first
array should come out like this
Now list1 = 24->18->3-7->null
Now list2 = 31->-9->5->21->4->0->8->null
Now lsit3 = 5->0->1->null

but i keep getting this output
Now list1= 12->3->7->null
Now list2= -7->null
Now list3= 5->0->1->null

int arr1[] = {24, 18, 12, 3, 7};
int arr2[] = {31, -9, 5, 21, 4, 0, 8, -7};
int arr3[] = {5, 5, 0, 1};

IntNode list1, list2, list3, list4, list5, list6;
list1 = createList(arr1);
list2 = createList(arr2);
list3 = createList(arr3);

System.out.println("to begin with, ");
System.out.println("list1= " + toString(list1));
System.out.println("list2= " + toString(list2));
System.out.println("list3= " + toString(list3));

list1 = remove(list1,12); // remove returns the new list so that list1 changes
list2 = remove(list2, -7); // invoke your method to remove -7 from list2
list3 = remove(list3, 5); // invoke your method to remove 5 from list3

  
System.out.println("Now list1= " + toString(list1));
System.out.println("Now list2= " + toString(list2));
System.out.println("Now list3= " + toString(list3));

So far my remove method looks like this:

public static IntNode remove(IntNode head,int value){
IntNode current = head;
IntNode prev = null;
if( current != null && current.data == value){
return head = current.next;
}
  
while( current != null && current.data != value){
//prev = current;
current = current.next;
}

prev = current;
return current;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the corrected code:

public static IntNode remove(IntNode head,int value){
IntNode current = head;
IntNode prev = head;
  

//This condition is to check if head is itself the value
if(current != null && current.data == value){
return current.next;
}
  

//Iterating till we get the node.data = value
while(current != null && current.data != value){
prev = current;
current = current.next;
}
  

// If current node's data is equal to value

// then join the previous node's next to current node's next
if(current != null && current.data == value){
prev.next = current.next;
}
  
return head;
}

Hope it helps!

Add a comment
Know the answer?
Add Answer to:
Java Given the array, which has been converted to a linked list, I need to invoke...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider java for fixing this code please: what i need is to insert method to be...

    Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...

  • Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a...

    Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList {    private class Node {        private Object data; //Assume data implemented Comparable        private Node next, prev;        private Node(Object data, Node pref, Node next)        {            this.data = data;       ...

  • Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k...

    Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • public class LinkedList {          // The LinkedList Node class    private class Node{...

    public class LinkedList {          // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)    {        this.head = new Node(gdata);    }...

  • Improve the speed of public T get(int i) by having it work backward from the end...

    Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> {    private static class Node<T> {        public Node<T> prev, next;...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • Java/LinkedList Need help with a few of the TODO parts, more info below in comments in...

    Java/LinkedList Need help with a few of the TODO parts, more info below in comments in bold. Thanks, package lab4; import java.util.IdentityHashMap; public class IntNode implements Cloneable {    private int data;    private IntNode next;       public IntNode(int d, IntNode n) {        data = d;        next = n;    }       public IntNode getNext() {        return next;    }          /// Override methods from Object       @Override   ...

  • JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main...

    JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks HashIntSet.java public class HashIntSet { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry[] elementData; private int size; // Constructs an empty set. public HashIntSet() { elementData = new HashEntry[10]; size = 0; } // Adds the given element to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT