Write a method move that moves a specified number of elements starting from a sp
ID: 3824255 • Letter: W
Question
Write a method move that moves a specified number of elements starting from a specified node to the end of the list. Your method should traverse through the linked list, list, until you find the ListNode which contains the second parameter, remove. The third parameter represents the number of ListNodes to remove after that ListNode and add to the end of the list. Your method should return the transformed linked list.
If no node matching remove is found, then the your method should return the original list.
The ListNode class will be accessible when your method is tested.
public class ListNode {
int info;
ListNode next;
ListNode(int x){ info = x; }
ListNode(int x, ListNode node){
info = x;
next =
Use the skeleton code below:
-------------------------------------------------
public class RemoveN {
public ListNode move(ListNode list, int remove, int n) {
// replace statement below with code you write
return null; } }
---------------------------------------------------------
Examples:
returns [1, 5, 2, 3, 4]
The node you're trying to find is 1, and you want to remove the three nodes after it. The nodes you want to remove are [2, 3, 4], so you return [1, 5, 2, 3, 4].
returns [1, 3, 9, 8, 2, 0]
The node you're trying to find is 1, and you want to remove the five nodes. There are exactly five nodes after the first node, so the list does not change.
Explanation / Answer
Hi, Please find my implementation.
class ListNode {
int info;
ListNode next;
ListNode(int x){
info = x;
}
ListNode(int x, ListNode node){
info = x;
next =node;
}
}
public class RemoveN {
public static ListNode move(ListNode list, int remove, int n) {
// replace statement below with code you write
// base case
if(list == null || list.next == null)
return list;
ListNode start = list;
ListNode end = list;
ListNode curent = list;
ListNode prev = null;
ListNode newHead = list;
// counting the length
int length = 0;
curent = list;
while(curent != null){
curent = curent.next;
length++;
}
// number of elements are less than n value
if(length <= n)
return list;
// finding removed node
curent = list;
while(curent!= null && curent.info != remove){
prev = curent;
curent = curent.next;
}
// removed element is not present
if(curent == null)
return list;
prev = curent;
// starting point of portion that will be removed
start = curent.next;
//System.out.println("Start: "+prev.info);
//System.out.println("Start: "+start.info);
// finding the end point of the portion to be removed
end = start;
int i = 0;
while(end != null && i<n-1){
end = end.next;
i++;
}
//System.out.println("End: "+end.info);
if(end == null || end.next == null)
return list;
ListNode temp = end.next;
// appending the rest of the portion from last
if(prev != null){
while(temp != null){
prev.next = temp;
prev = temp;
temp = temp.next;
}
}
end.next = null;
//appending the removed portion at last
while(start != end){
prev.next = start;
prev = start;
start = start.next;
}
prev.next = end;
return newHead;
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
head.next = new ListNode(3);
head.next.next = new ListNode(9);
head.next.next.next = new ListNode(8);
head.next.next.next.next = new ListNode(2);
head.next.next.next.next.next = new ListNode(0);
ListNode removed = move(head, 1, 5);
while(removed != null){
System.out.print(removed.info+" ");
removed = removed.next;
}
}
}
/*
Sample run:
1 3 9 8 2 0
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.