12
12
*/
13
13
public class RemoveMiddlePointsFromLineSegments {
14
14
15
+ /**
16
+ * Given a linked list of co-ordinates representing line segments, we have
17
+ * to remove those nodes which represent the middle points.
18
+ * <p/>
19
+ * Example:
20
+ * Input:
21
+ * (0,10)-> (1,10)-> (3,10)-> (10,10)-> (10,8)-> (10,5)-> (20,5)-> (40,5)
22
+ * Output:
23
+ * (0,10)-> (10,10)-> (10,5)-> (40,5)
24
+ * <p/>
25
+ * Input:
26
+ * (2,3)->(4,3)->(6,3)->(10,3)->(12,3)
27
+ * Output:
28
+ * (2,3)->(12,3)
29
+ *
30
+ * @param node
31
+ */
15
32
public static void removeMiddlePointsFromLineSegments (SingleLinkedNode <Point > node ) {
16
33
17
34
SingleLinkedNode <Point > curr1 = node , curr2 = node ;
@@ -29,13 +46,15 @@ public static void removeMiddlePointsFromLineSegments(SingleLinkedNode<Point> no
29
46
}
30
47
curr1 .next = curr2 ;
31
48
} else {
49
+ System .out .println ("Linked list doesn't represent line segments!" );
32
50
return ;
33
51
}
34
52
curr1 = curr1 .next ;
35
53
}
36
54
}
37
55
38
56
public static void main (String a []) {
57
+ // test case 1
39
58
SingleLinkedList <Point > linkedList = new SingleLinkedList <>();
40
59
linkedList .add (new Point (0 , 10 ));
41
60
linkedList .add (new Point (1 , 10 ));
@@ -48,6 +67,17 @@ public static void main(String a[]) {
48
67
linkedList .printList ();
49
68
removeMiddlePointsFromLineSegments (linkedList .head );
50
69
linkedList .printList ();
70
+
71
+ // test case 2
72
+ SingleLinkedList <Point > linkedList2 = new SingleLinkedList <>();
73
+ linkedList2 .add (new Point (2 , 3 ));
74
+ linkedList2 .add (new Point (4 , 3 ));
75
+ linkedList2 .add (new Point (6 , 3 ));
76
+ linkedList2 .add (new Point (10 , 3 ));
77
+ linkedList2 .add (new Point (12 , 3 ));
78
+ linkedList2 .printList ();
79
+ removeMiddlePointsFromLineSegments (linkedList2 .head );
80
+ linkedList2 .printList ();
51
81
}
52
82
}
53
83
0 commit comments