Given a Linked List. Write a Program to retain n elements and delete m elements till the end of the List.

Consider a Linked List is given to you. And you have to retain n elements and delete m elements till the end of the List. To understand the problem with more clarity consider the following example.

Input: N = 1, M = 1 Linked List: 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10

Here you have to retain 1 element and delete 1 element till the end of the LinkedList. If we perform the operation in the given list, then the result will be :

[Retain 1] [Delete 2] [Retain 3] [Delete 4] .... end of the list = 1 → 3 → 5 → 7 → 9

Consider some other values of N and M in the given LinkedList.

CASE 2:
/*
 * Input: N = 3, M = 2 Linked List: 1->2->3->4->5->6->7->8->9->10 Output:
 * Linked List: 1->2->3->6->7->8
 */
 
CASE 3:
/*
 * Input: N = 2, M = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked
 * List: 1->2->5->6
 */

Now let us check the Java program to implement the same.

package com.linked.list;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.junit.Test;

public class RetainNAndDeleteM {

	/**
	 * From the list retain n and delete m till the end of the list
	 * 
	 * @param list
	 * @param n
	 * @param m
	 */
	private static void retainNDeleteM(List<Integer> list, int n, int m) {
		if (list == null || list.isEmpty())
			return;
		int tempN = n;
		int tempM = m;
		Iterator<Integer> listItr = list.iterator();
		while (listItr.hasNext()) {
			if (tempN > 0) {
				listItr.next();
				tempN--;
			} else if (tempM > 0) {
				listItr.next();
				listItr.remove();
				tempM--;
			} else {
				tempN = tempN == 0 ? n : tempN;
				tempM = tempM == 0 ? m : tempM;
			}
		}
	}

	/*
	 * Input: N = 1, M = 1 Linked List: 1->2->3->4->5->6->7->8->9->10 Output:
	 * Linked List: 1->3->5->7->9
	 */
	@Test
	public void Test3() {
		List<Integer> list = getList(10);
		retainNDeleteM(list, 1, 1);
		System.out.println(list);
	}

	/*
	 * Input: N = 3, M = 2 Linked List: 1->2->3->4->5->6->7->8->9->10 Output:
	 * Linked List: 1->2->3->6->7->8
	 */
	@Test
	public void Test2() {
		List<Integer> list = getList(10);
		retainNDeleteM(list, 3, 2);
		System.out.println(list);
	}

	/*
	 * Input: N = 2, M = 2 Linked List: 1->2->3->4->5->6->7->8 Output: Linked
	 * List: 1->2->5->6
	 */
	@Test
	public void Test1() {
		List<Integer> list = getList(8);
		retainNDeleteM(list, 2, 2);
		System.out.println(list);
	}

	private List<Integer> getList(int val) {
		List<Integer> list = new LinkedList<>();
		for (int i = 1; i <= val; i++) {
			list.add(i);
		}
		return list;
	}
}

 

core java 12 Algorithm 12

FOLLOW US ON LinkedIn



Explore Tutu'rself