Skip to content

update SinglyLinkedList (#2058) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions DataStructures/Lists/SinglyLinkedList.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package DataStructures.Lists;

/**
* This class implements a SinglyLinked List. This is done using SinglyLinkedList class and a
* LinkForLinkedList Class.
*
* <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.
*/
import java.util.StringJoiner;

/** https://en.wikipedia.org/wiki/Linked_list */
public class SinglyLinkedList {
/** Head refer to the front of the list */
private Node head;
Expand Down Expand Up @@ -213,16 +207,13 @@ public int getNth(int index) {

@Override
public String toString() {
if (size == 0) {
return "";
}
StringBuilder builder = new StringBuilder();
StringJoiner joiner = new StringJoiner("->");
Node cur = head;
while (cur != null) {
builder.append(cur.value).append("->");
joiner.add(cur.value + "");
cur = cur.next;
}
return builder.replace(builder.length() - 2, builder.length(), "").toString();
return joiner.toString();
}

/** Driver Code */
Expand Down