patternjavaMinor
Repeatedly skip M nodes then delete N nodes then skip M nodes and so on
Viewed 0 times
nodesdeleteskipthenandrepeatedly
Problem
Given a linked list and two integers M and N. Traverse the linked list
such that you retain M nodes then delete next N nodes, continue the
same until end of the linked list.
-
Input:
M = 2, N = 2
Linked List: 1->2->3->4->5->6->7->8
-
Output:
Linked List: 1->2->5->6
I'm looking for code review, optimizations and best practices.
```
public class DeleteNAfterM {
private Node first;
private Node last;
private int size;
public DeleteNAfterM(List list) {
for (T item : list) {
create(item);
}
}
private void create(T item) {
Node node = new Node(item);
Node l = last;
if (first == null) {
first = node;
} else {
l.next = node;
}
last = node;
size++;
}
private static class Node {
private T item;
private Node next;
Node(T item) {
this.item = item;
}
}
public void deleteNAfterM(int m, int n) {
if (first == null) {
throw new IllegalArgumentException(" the linkedlist should not empty.");
}
if (m 0) {
size = 0;
first = last = null;
return;
}
Node node = first;
while (node != null) {
for (int i = 1; i temp = node;
for (int i = 0; i x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
}
public class DeleteNAfterMTest {
@Test
public void test1() {
// last node is not deleted
DeleteNAfterM deleteMAfterN1 = new DeleteNAfterM(Arrays.asList(1, 2, 3, 4, 5, 6));
deleteMAfterN1.deleteNAfterM(2, 2);
Object[] itemList1 = deleteMAfterN1.toArray();
Integer[] expected1 = {1, 2, 5, 6};
assertTrue(Arrays.equals(expected1, Arrays.asList(itemList1).toArray(new Integer[itemList1.length])));
}
@Test
public void test2() {
such that you retain M nodes then delete next N nodes, continue the
same until end of the linked list.
-
Input:
M = 2, N = 2
Linked List: 1->2->3->4->5->6->7->8
-
Output:
Linked List: 1->2->5->6
I'm looking for code review, optimizations and best practices.
```
public class DeleteNAfterM {
private Node first;
private Node last;
private int size;
public DeleteNAfterM(List list) {
for (T item : list) {
create(item);
}
}
private void create(T item) {
Node node = new Node(item);
Node l = last;
if (first == null) {
first = node;
} else {
l.next = node;
}
last = node;
size++;
}
private static class Node {
private T item;
private Node next;
Node(T item) {
this.item = item;
}
}
public void deleteNAfterM(int m, int n) {
if (first == null) {
throw new IllegalArgumentException(" the linkedlist should not empty.");
}
if (m 0) {
size = 0;
first = last = null;
return;
}
Node node = first;
while (node != null) {
for (int i = 1; i temp = node;
for (int i = 0; i x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
}
public class DeleteNAfterMTest {
@Test
public void test1() {
// last node is not deleted
DeleteNAfterM deleteMAfterN1 = new DeleteNAfterM(Arrays.asList(1, 2, 3, 4, 5, 6));
deleteMAfterN1.deleteNAfterM(2, 2);
Object[] itemList1 = deleteMAfterN1.toArray();
Integer[] expected1 = {1, 2, 5, 6};
assertTrue(Arrays.equals(expected1, Arrays.asList(itemList1).toArray(new Integer[itemList1.length])));
}
@Test
public void test2() {
Solution
You should probably just use Java's LinkedList. If you were working for some company, it would not be well seen if you spent some time reinventing the wheel.
Now, for something that is completely off topic:
You seem to have a whole bank of interview questions which you are answering and submitting on codereview. Those questions seem kind of old. You could play around with newer technology. It's fun and, if you want to get hired, it shows you like programming (even if you will not use that technology in your work). For example, I am answering your question with Java 8 below. Stream is not a linked list, but it is related: It's basically a lazily evaluated linked list.
Now, for something that is completely off topic:
You seem to have a whole bank of interview questions which you are answering and submitting on codereview. Those questions seem kind of old. You could play around with newer technology. It's fun and, if you want to get hired, it shows you like programming (even if you will not use that technology in your work). For example, I am answering your question with Java 8 below. Stream is not a linked list, but it is related: It's basically a lazily evaluated linked list.
private static Stream filterMAfterN(Stream stream, int mValid, int nInvalid) {
final AtomicInteger index = new AtomicInteger(0);
return stream.filter(unusedValue -> {
int rescaledIndex = index.getAndIncrement() % (mValid + nInvalid);
return rescaledIndex stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
System.out.println(filterMAfterN(stream, 1, 2).collect(Collectors.toList()));
}Code Snippets
private static <T> Stream<T> filterMAfterN(Stream<T> stream, int mValid, int nInvalid) {
final AtomicInteger index = new AtomicInteger(0);
return stream.filter(unusedValue -> {
int rescaledIndex = index.getAndIncrement() % (mValid + nInvalid);
return rescaledIndex < mValid;
});
}
public static void main(String[] args) {
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
System.out.println(filterMAfterN(stream, 1, 2).collect(Collectors.toList()));
}Context
StackExchange Code Review Q#56045, answer score: 7
Revisions (0)
No revisions yet.