Skip to content

Commit 53b2b69

Browse files
authored
Merge pull request TheAlgorithms#713 from ojasiiitd/patch-2
Major Updates in SinglyLinkedList.java
2 parents 1d02cd4 + 6c4fd0e commit 53b2b69

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

DataStructures/Lists/SinglyLinkedList.java

+54-21
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ class SinglyLinkedList {
1717
*/
1818
private Node head;
1919

20-
/**
21-
* Count of nodes
22-
*/
23-
private int count;
24-
2520
/**
2621
* This method inserts an element at the head
2722
*
@@ -31,7 +26,6 @@ public void insertHead(int x) {
3126
Node newNode = new Node(x);
3227
newNode.next = head;
3328
head = newNode;
34-
++count;
3529
}
3630

3731
/**
@@ -42,35 +36,51 @@ public void insertHead(int x) {
4236
*/
4337

4438
public void insertNth(int data, int position) {
45-
if (position < 0 || position > count) {
39+
if (position < 0 || position > getSize()) {
4640
throw new RuntimeException("position less than zero or position more than the count of list");
4741
}
48-
Node node = new Node(data);
49-
Node dummy = new Node(-1);
50-
dummy.next = head;
51-
Node cur = dummy;
52-
for (int i = 0; i < position; ++i) {
53-
cur = cur.next;
42+
else if (position == 0)
43+
insertHead(data);
44+
else {
45+
Node cur = head;
46+
Node node = new Node(data);
47+
for (int i = 1; i < position; ++i) {
48+
cur = cur.next;
49+
}
50+
node.next = cur.next;
51+
cur.next = node;
5452
}
55-
node.next = cur.next;
56-
cur.next = node;
57-
++count;
5853
}
5954

6055
/**
6156
* This method deletes an element at the head
6257
*
6358
* @return The element deleted
6459
*/
65-
public Node deleteHead() {
60+
public void deleteHead() {
6661
if (isEmpty()) {
6762
throw new RuntimeException("The list is empty!");
6863
}
6964

70-
Node temp = head;
7165
head = head.next;
72-
--count;
73-
return temp;
66+
}
67+
68+
/**
69+
* This method deletes an element at Nth position
70+
*/
71+
public void deleteNth(int position) {
72+
if (position < 0 || position > getSize()) {
73+
throw new RuntimeException("position less than zero or position more than the count of list");
74+
}
75+
else if (position == 0)
76+
deleteHead();
77+
else {
78+
Node cur = head;
79+
for (int i = 1; i < position; ++i) {
80+
cur = cur.next;
81+
}
82+
cur.next = cur.next.next;
83+
}
7484
}
7585

7686
/**
@@ -79,7 +89,7 @@ public Node deleteHead() {
7989
* @return true is list is empty
8090
*/
8191
public boolean isEmpty() {
82-
return count == 0;
92+
return getSize() == 0;
8393
}
8494

8595
/**
@@ -94,6 +104,23 @@ public void display() {
94104
System.out.println();
95105
}
96106

107+
/**
108+
* Returns the size of the linked list
109+
*/
110+
public int getSize() {
111+
if (head == null)
112+
return 0;
113+
else {
114+
Node current = head;
115+
int size = 1;
116+
while (current.next != null) {
117+
current = current.next;
118+
size++;
119+
}
120+
return size;
121+
}
122+
}
123+
97124
/**
98125
* Main method
99126
*
@@ -117,6 +144,11 @@ public static void main(String args[]) {
117144
myList.insertNth(11, 2);
118145

119146
myList.display(); // 7 -> 5 -> 11
147+
148+
myList.deleteNth(1);
149+
150+
myList.display(); // 7-> 11
151+
120152
}
121153
}
122154

@@ -145,5 +177,6 @@ class Node {
145177
*/
146178
Node(int value) {
147179
this.value = value;
180+
this.next = null;
148181
}
149182
}

0 commit comments

Comments
 (0)