File tree 1 file changed +6
-15
lines changed
1 file changed +6
-15
lines changed Original file line number Diff line number Diff line change 1
1
package DataStructures .Lists ;
2
2
3
- /**
4
- * This class implements a SinglyLinked List. This is done using SinglyLinkedList class and a
5
- * LinkForLinkedList Class.
6
- *
7
- * <p>A linked list is similar to an array, it hold values. However, links in a linked list do not
8
- * have indexes. With a linked list you do not need to predetermine it's size as it grows and
9
- * shrinks as it is edited. This is an example of a singly linked list. Elements can only be
10
- * added/removed at the head/front of the list.
11
- */
3
+ import java .util .StringJoiner ;
4
+
5
+ /** https://en.wikipedia.org/wiki/Linked_list */
12
6
public class SinglyLinkedList {
13
7
/** Head refer to the front of the list */
14
8
private Node head ;
@@ -213,16 +207,13 @@ public int getNth(int index) {
213
207
214
208
@ Override
215
209
public String toString () {
216
- if (size == 0 ) {
217
- return "" ;
218
- }
219
- StringBuilder builder = new StringBuilder ();
210
+ StringJoiner joiner = new StringJoiner ("->" );
220
211
Node cur = head ;
221
212
while (cur != null ) {
222
- builder . append (cur .value ). append ( "-> " );
213
+ joiner . add (cur .value + " " );
223
214
cur = cur .next ;
224
215
}
225
- return builder . replace ( builder . length () - 2 , builder . length (), "" ) .toString ();
216
+ return joiner .toString ();
226
217
}
227
218
228
219
/** Driver Code */
You can’t perform that action at this time.
0 commit comments