Skip to content

Major Updates in SinglyLinkedList.java #713

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
Mar 9, 2019
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
75 changes: 54 additions & 21 deletions DataStructures/Lists/SinglyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ class SinglyLinkedList {
*/
private Node head;

/**
* Count of nodes
*/
private int count;

/**
* This method inserts an element at the head
*
Expand All @@ -31,7 +26,6 @@ public void insertHead(int x) {
Node newNode = new Node(x);
newNode.next = head;
head = newNode;
++count;
}

/**
Expand All @@ -42,35 +36,51 @@ public void insertHead(int x) {
*/

public void insertNth(int data, int position) {
if (position < 0 || position > count) {
if (position < 0 || position > getSize()) {
throw new RuntimeException("position less than zero or position more than the count of list");
}
Node node = new Node(data);
Node dummy = new Node(-1);
dummy.next = head;
Node cur = dummy;
for (int i = 0; i < position; ++i) {
cur = cur.next;
else if (position == 0)
insertHead(data);
else {
Node cur = head;
Node node = new Node(data);
for (int i = 1; i < position; ++i) {
cur = cur.next;
}
node.next = cur.next;
cur.next = node;
}
node.next = cur.next;
cur.next = node;
++count;
}

/**
* This method deletes an element at the head
*
* @return The element deleted
*/
public Node deleteHead() {
public void deleteHead() {
if (isEmpty()) {
throw new RuntimeException("The list is empty!");
}

Node temp = head;
head = head.next;
--count;
return temp;
}

/**
* This method deletes an element at Nth position
*/
public void deleteNth(int position) {
if (position < 0 || position > getSize()) {
throw new RuntimeException("position less than zero or position more than the count of list");
}
else if (position == 0)
deleteHead();
else {
Node cur = head;
for (int i = 1; i < position; ++i) {
cur = cur.next;
}
cur.next = cur.next.next;
}
}

/**
Expand All @@ -79,7 +89,7 @@ public Node deleteHead() {
* @return true is list is empty
*/
public boolean isEmpty() {
return count == 0;
return getSize() == 0;
}

/**
Expand All @@ -94,6 +104,23 @@ public void display() {
System.out.println();
}

/**
* Returns the size of the linked list
*/
public int getSize() {
if (head == null)
return 0;
else {
Node current = head;
int size = 1;
while (current.next != null) {
current = current.next;
size++;
}
return size;
}
}

/**
* Main method
*
Expand All @@ -117,6 +144,11 @@ public static void main(String args[]) {
myList.insertNth(11, 2);

myList.display(); // 7 -> 5 -> 11

myList.deleteNth(1);

myList.display(); // 7-> 11

}
}

Expand Down Expand Up @@ -145,5 +177,6 @@ class Node {
*/
Node(int value) {
this.value = value;
this.next = null;
}
}