The objective:
Write a method called removeEvens that removes the values in even-numbered indexes from a list, returning a new list that contains those values in their original order.
For example, consider a variable
list1 that stores values { 1, 2, 3, 4, 5, 6 }
If the following call is made:
LinkedIntList list 2 = list1.removeEvens();
Then --
List1 { 2, 4, 6 }
List2 { 1, 3, 5 }
Restrictions: You may not call any methods of the class other than the constructor.
You may not create new nodes nor change the values stored in data fields.
You must solve it by rearranging the links of the list.
The greatest trouble i have with this problem is that I can only work with the two lists at hand and can't create any new nodes or a like. I'm having a hard time trying to think how I could get the program to actually loop through the list and change up the links
So far I have made two test cases to check - if the list either has no data, or has only one data.
My Question: How can I loop through the initial list A, and relink all the nodes without actually creating new nodes, without calling other methods, cannot change the datafields. How can i rearrange the links for any number of # data in a list. Any pointers will be greatly appreciated! I just can't wrap my mind around this one when it deals with X amount of data beyond 1 data set.
public LinkedIntList removeEvens(){ LinkedIntList b = new LinkedIntList(); b.front=front; // Points list B to front. if(front == null) { // If list is empty, output notice. System.out.println("The list inputed it empty"); }if(front.next == null){// If list has one value, give it to list B and remove from list A. front = null; }else{ // If the list has two or more values, do this. front = front.next; b.front.next = b.front.next.next; while(front.next != null){ } } return b; } }