File tree Expand file tree Collapse file tree 5 files changed +164
-0
lines changed
collections/linkedlist/remove-element-from-linkedlist Expand file tree Collapse file tree 5 files changed +164
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+
5+ /**
6+ * A Java program to remove head element from
7+ * the linkedList using the remove() method.
8+ *
9+ * @author coderolls.com
10+ *
11+ */
12+ public class LinkedListRemove {
13+
14+ public static void main (String [] args ) {
15+
16+ LinkedList <String > linkedList = new LinkedList <String >();
17+
18+ linkedList .add ("BHIM" );
19+ linkedList .add ("PayTM" );
20+ linkedList .add ("Freecharge" );
21+ linkedList .add ("PhonePe" );
22+ linkedList .add ("JioPay" );
23+
24+ System .out .println ("LinkedList before removing an element:" );
25+ System .out .println (linkedList );
26+
27+ // removes head(first element) i.e. removes BHIM
28+ linkedList .remove ();
29+
30+ System .out .println ("\n LinkedList after removing an element using remove() method:" );
31+ System .out .println (linkedList );
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+
5+ /**
6+ * A Java program to remove first element from
7+ * the linkedList using the removeFirst() method.
8+ *
9+ * @author coderolls.com
10+ *
11+ */
12+ public class LinkedListRemoveFirst {
13+
14+ public static void main (String [] args ) {
15+
16+ LinkedList <String > linkedList = new LinkedList <String >();
17+
18+ linkedList .add ("BHIM" );
19+ linkedList .add ("PayTM" );
20+ linkedList .add ("Freecharge" );
21+ linkedList .add ("PhonePe" );
22+ linkedList .add ("JioPay" );
23+
24+ System .out .println ("LinkedList before removing an element:" );
25+ System .out .println (linkedList );
26+
27+ // removes first element i.e. removes BHIM
28+ linkedList .removeFirst ();
29+
30+ System .out .println ("\n LinkedList after removing an element using removeFirst() method:" );
31+ System .out .println (linkedList );
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+
5+ /**
6+ * A Java program to remove last element from
7+ * the linkedList using the removeLast() method.
8+ *
9+ * @author coderolls.com
10+ *
11+ */
12+ public class LinkedListRemoveLast {
13+
14+ public static void main (String [] args ) {
15+
16+ LinkedList <String > linkedList = new LinkedList <String >();
17+
18+ linkedList .add ("BHIM" );
19+ linkedList .add ("PayTM" );
20+ linkedList .add ("Freecharge" );
21+ linkedList .add ("PhonePe" );
22+ linkedList .add ("JioPay" );
23+
24+ System .out .println ("LinkedList before removing an element:" );
25+ System .out .println (linkedList );
26+
27+ // removes first element i.e. removes BHIM
28+ linkedList .removeLast ();
29+
30+ System .out .println ("\n LinkedList after removing an element using removeLast() method:" );
31+ System .out .println (linkedList );
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+
5+ /**
6+ * A Java program to remove the specified element from
7+ * the linkedList using the remove(Object o) method.
8+ *
9+ * @author coderolls.com
10+ *
11+ */
12+ public class LinkedListRemoveObject {
13+
14+ public static void main (String [] args ) {
15+
16+ LinkedList <String > linkedList = new LinkedList <String >();
17+
18+ linkedList .add ("BHIM" );
19+ linkedList .add ("PayTM" );
20+ linkedList .add ("Freecharge" );
21+ linkedList .add ("PhonePe" );
22+ linkedList .add ("JioPay" );
23+
24+ System .out .println ("LinkedList before removing an element:" );
25+ System .out .println (linkedList );
26+
27+ // removes the specified element i.e. removes Freecharge
28+ linkedList .remove ("Freecharge" );
29+
30+ System .out .println ("\n LinkedList after removing an element using remove(Object o) method:" );
31+ System .out .println (linkedList );
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .gaurav .ExProject .LinkedList ;
2+
3+ import java .util .LinkedList ;
4+
5+ /**
6+ * A Java program to remove element at the specified position i.e. index
7+ * from the linkedList using the remove(int index) method.
8+ *
9+ * @author coderolls.com
10+ */
11+ public class LinkedListRemoveObjectByIndex {
12+
13+ public static void main (String [] args ) {
14+
15+ LinkedList <String > linkedList = new LinkedList <String >();
16+
17+ linkedList .add ("BHIM" );
18+ linkedList .add ("PayTM" );
19+ linkedList .add ("Freecharge" );
20+ linkedList .add ("PhonePe" );
21+ linkedList .add ("JioPay" );
22+
23+ System .out .println ("LinkedList before removing an element:" );
24+ System .out .println (linkedList );
25+
26+ // removes the element at index 3 i.e. removes PhonePe
27+ linkedList .remove (3 );
28+
29+ System .out .println ("\n LinkedList after removing an element using remove(int index) method:" );
30+ System .out .println (linkedList );
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments