update SinglyLinkedList (#2058)
This commit is contained in:
parent
c85223bfca
commit
95484f8c89
@ -1,14 +1,8 @@
|
|||||||
package DataStructures.Lists;
|
package DataStructures.Lists;
|
||||||
|
|
||||||
/**
|
import java.util.StringJoiner;
|
||||||
* This class implements a SinglyLinked List. This is done using SinglyLinkedList class and a
|
|
||||||
* LinkForLinkedList Class.
|
/** https://en.wikipedia.org/wiki/Linked_list */
|
||||||
*
|
|
||||||
* <p>A linked list is similar to an array, it hold values. However, links in a linked list do not
|
|
||||||
* have indexes. With a linked list you do not need to predetermine it's size as it grows and
|
|
||||||
* shrinks as it is edited. This is an example of a singly linked list. Elements can only be
|
|
||||||
* added/removed at the head/front of the list.
|
|
||||||
*/
|
|
||||||
public class SinglyLinkedList {
|
public class SinglyLinkedList {
|
||||||
/** Head refer to the front of the list */
|
/** Head refer to the front of the list */
|
||||||
private Node head;
|
private Node head;
|
||||||
@ -213,16 +207,13 @@ public class SinglyLinkedList {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (size == 0) {
|
StringJoiner joiner = new StringJoiner("->");
|
||||||
return "";
|
|
||||||
}
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
Node cur = head;
|
Node cur = head;
|
||||||
while (cur != null) {
|
while (cur != null) {
|
||||||
builder.append(cur.value).append("->");
|
joiner.add(cur.value + "");
|
||||||
cur = cur.next;
|
cur = cur.next;
|
||||||
}
|
}
|
||||||
return builder.replace(builder.length() - 2, builder.length(), "").toString();
|
return joiner.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Driver Code */
|
/** Driver Code */
|
||||||
|
Loading…
Reference in New Issue
Block a user