Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit53b2b69

Browse files
authored
Merge pull requestTheAlgorithms#713 from ojasiiitd/patch-2
Major Updates in SinglyLinkedList.java
2 parents1d02cd4 +6c4fd0e commit53b2b69

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

‎DataStructures/Lists/SinglyLinkedList.java

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ class SinglyLinkedList {
1717
*/
1818
privateNodehead;
1919

20-
/**
21-
* Count of nodes
22-
*/
23-
privateintcount;
24-
2520
/**
2621
* This method inserts an element at the head
2722
*
@@ -31,7 +26,6 @@ public void insertHead(int x) {
3126
NodenewNode =newNode(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
publicvoidinsertNth(intdata,intposition) {
45-
if (position <0 ||position >count) {
39+
if (position <0 ||position >getSize()) {
4640
thrownewRuntimeException("position less than zero or position more than the count of list");
4741
}
48-
Nodenode =newNode(data);
49-
Nodedummy =newNode(-1);
50-
dummy.next =head;
51-
Nodecur =dummy;
52-
for (inti =0;i <position; ++i) {
53-
cur =cur.next;
42+
elseif (position ==0)
43+
insertHead(data);
44+
else {
45+
Nodecur =head;
46+
Nodenode =newNode(data);
47+
for (inti =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-
publicNodedeleteHead() {
60+
publicvoiddeleteHead() {
6661
if (isEmpty()) {
6762
thrownewRuntimeException("The list is empty!");
6863
}
6964

70-
Nodetemp =head;
7165
head =head.next;
72-
--count;
73-
returntemp;
66+
}
67+
68+
/**
69+
* This method deletes an element at Nth position
70+
*/
71+
publicvoiddeleteNth(intposition) {
72+
if (position <0 ||position >getSize()) {
73+
thrownewRuntimeException("position less than zero or position more than the count of list");
74+
}
75+
elseif (position ==0)
76+
deleteHead();
77+
else {
78+
Nodecur =head;
79+
for (inti =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
publicbooleanisEmpty() {
82-
returncount ==0;
92+
returngetSize() ==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+
publicintgetSize() {
111+
if (head ==null)
112+
return0;
113+
else {
114+
Nodecurrent =head;
115+
intsize =1;
116+
while (current.next !=null) {
117+
current =current.next;
118+
size++;
119+
}
120+
returnsize;
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(intvalue) {
147179
this.value =value;
180+
this.next =null;
148181
}
149182
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp