@@ -17,11 +17,6 @@ class SinglyLinkedList {
17
17
*/
18
18
private Node head ;
19
19
20
- /**
21
- * Count of nodes
22
- */
23
- private int count ;
24
-
25
20
/**
26
21
* This method inserts an element at the head
27
22
*
@@ -31,7 +26,6 @@ public void insertHead(int x) {
31
26
Node newNode = new Node (x );
32
27
newNode .next = head ;
33
28
head = newNode ;
34
- ++count ;
35
29
}
36
30
37
31
/**
@@ -42,35 +36,51 @@ public void insertHead(int x) {
42
36
*/
43
37
44
38
public void insertNth (int data , int position ) {
45
- if (position < 0 || position > count ) {
39
+ if (position < 0 || position > getSize () ) {
46
40
throw new RuntimeException ("position less than zero or position more than the count of list" );
47
41
}
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 ;
54
52
}
55
- node .next = cur .next ;
56
- cur .next = node ;
57
- ++count ;
58
53
}
59
54
60
55
/**
61
56
* This method deletes an element at the head
62
57
*
63
58
* @return The element deleted
64
59
*/
65
- public Node deleteHead () {
60
+ public void deleteHead () {
66
61
if (isEmpty ()) {
67
62
throw new RuntimeException ("The list is empty!" );
68
63
}
69
64
70
- Node temp = head ;
71
65
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
+ }
74
84
}
75
85
76
86
/**
@@ -79,7 +89,7 @@ public Node deleteHead() {
79
89
* @return true is list is empty
80
90
*/
81
91
public boolean isEmpty () {
82
- return count == 0 ;
92
+ return getSize () == 0 ;
83
93
}
84
94
85
95
/**
@@ -94,6 +104,23 @@ public void display() {
94
104
System .out .println ();
95
105
}
96
106
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
+
97
124
/**
98
125
* Main method
99
126
*
@@ -117,6 +144,11 @@ public static void main(String args[]) {
117
144
myList .insertNth (11 , 2 );
118
145
119
146
myList .display (); // 7 -> 5 -> 11
147
+
148
+ myList .deleteNth (1 );
149
+
150
+ myList .display (); // 7-> 11
151
+
120
152
}
121
153
}
122
154
@@ -145,5 +177,6 @@ class Node {
145
177
*/
146
178
Node (int value ) {
147
179
this .value = value ;
180
+ this .next = null ;
148
181
}
149
182
}
0 commit comments