File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
main/java/br/com/zevolution/datastructure/linkedlist
test/java/br/com/zevolution/datastructure/linkedlist Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,33 @@ public void removeFirst() {
58
58
}
59
59
}
60
60
61
+ public void remove (Object element ) {
62
+ Node current = this .first ;
63
+ Node previous = null ;
64
+
65
+ if (current .getElement () == element ) {
66
+ this .first = current .getNext ();
67
+ current = null ;
68
+ this .totalElements --;
69
+ return ;
70
+ }
71
+
72
+ while (current != null ) {
73
+ if (current .getElement () == element ) {
74
+ break ;
75
+ }
76
+
77
+ previous = current ;
78
+ current = current .getNext ();
79
+ }
80
+
81
+ if (current == null ) return ;
82
+
83
+ previous .setNext (current .getNext ());
84
+ current = null ;
85
+ this .totalElements --;
86
+ }
87
+
61
88
public Object get (int position ) {
62
89
return this .getNode (position ).getElement ();
63
90
}
Original file line number Diff line number Diff line change @@ -93,4 +93,18 @@ public void should_ThrowException_When_NoSuchElement() {
93
93
linkedList .removeFirst ();
94
94
}
95
95
96
+ @ Test
97
+ public void should_RemoveMiddleElement () {
98
+ CustomLinkedList linkedList = new CustomLinkedList ();
99
+ linkedList .add ("Bia" );
100
+ linkedList .add ("Lucas" );
101
+ linkedList .add ("Laura" );
102
+
103
+ linkedList .remove ("Lucas" );
104
+
105
+ assertEquals (2 , linkedList .size ());
106
+ assertEquals ("Bia" , linkedList .get (0 ));
107
+ assertEquals ("Laura" , linkedList .get (1 ));
108
+ }
109
+
96
110
}
You can’t perform that action at this time.
0 commit comments