File tree Expand file tree Collapse file tree 1 file changed +67
-0
lines changed
src/me/ramswaroop/linkedlists Expand file tree Collapse file tree 1 file changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ package me .ramswaroop .linkedlists ;
2
+
3
+ import me .ramswaroop .common .SingleLinkedList ;
4
+ import me .ramswaroop .common .SingleLinkedNode ;
5
+
6
+ /**
7
+ * Created by IntelliJ IDEA.
8
+ *
9
+ * @author: ramswaroop
10
+ * @date: 7/6/15
11
+ * @time: 7:43 PM
12
+ */
13
+ public class DeleteMnodesAfterNnodes {
14
+
15
+ /**
16
+ * Deletes {@param n} nodes after every {@param m} nodes in {@param list}
17
+ * till it reaches the end of {@param list}.
18
+ *
19
+ * @param list
20
+ * @param m
21
+ * @param n
22
+ * @param <E>
23
+ */
24
+ public static <E extends Comparable <E >> void deleteMnodesAfterNnodes (SingleLinkedList <E > list ,
25
+ int m , int n ) {
26
+
27
+ SingleLinkedNode <E > curr1 = list .head , curr2 ;
28
+
29
+ while (curr1 != null ) {
30
+
31
+ // skip m nodes
32
+ for (int i = 1 ; curr1 .next != null && i < m ; i ++) {
33
+ curr1 = curr1 .next ;
34
+ }
35
+
36
+ // delete n nodes
37
+ curr2 = curr1 ;
38
+ for (int i = 0 ; curr2 != null && i <= n ; i ++) {
39
+ curr2 = curr2 .next ;
40
+ }
41
+ curr1 .next = curr2 ;
42
+
43
+ curr1 = curr1 .next ;
44
+ }
45
+ }
46
+
47
+ public static void main (String a []) {
48
+ SingleLinkedList <Integer > linkedList = new SingleLinkedList <>();
49
+ linkedList .add (7 );
50
+ linkedList .add (5 );
51
+ linkedList .add (9 );
52
+ linkedList .add (4 );
53
+ linkedList .add (6 );
54
+ linkedList .add (1 );
55
+ linkedList .add (2 );
56
+ linkedList .add (7 );
57
+ linkedList .add (5 );
58
+ linkedList .add (9 );
59
+ linkedList .add (4 );
60
+ linkedList .add (6 );
61
+ linkedList .add (1 );
62
+ linkedList .add (2 );
63
+ linkedList .printList ();
64
+ deleteMnodesAfterNnodes (linkedList , 3 , 2 );
65
+ linkedList .printList ();
66
+ }
67
+ }
You can’t perform that action at this time.
0 commit comments