Skip to content

Commit 9e0cdf3

Browse files
committed
tried to get link updated
1 parent 3dce7f0 commit 9e0cdf3

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* This implementation uses a singly linked list to store the elements.
99
* Further details can be found on this link
1010
* https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html
11-
*
1211
* @author Muhammad Junaid Khalid
1312
* @param int the type of elements in this list
1413
*/
@@ -24,24 +23,20 @@ public SortedLinkedList() {
2423

2524
/**
2625
* Inserts a new element into the sorted linked list.
27-
*
2826
* @param value the value to be inserted
2927
*/
3028
public void insert(int value) {
3129
Node newNode = new Node(value);
3230
if (head == null) {
3331
this.head = newNode;
3432
this.tail = newNode;
35-
}
36-
else if (value < head.value) {
33+
} else if (value < head.value) {
3734
newNode.next = this.head;
3835
this.head = newNode;
39-
}
40-
else if (value > tail.value) {
36+
} else if (value > tail.value) {
4137
this.tail.next = newNode;
4238
this.tail = newNode;
43-
}
44-
else{
39+
} else {
4540
Node temp = head;
4641
while (temp.next != null && temp.next.value < value) {
4742
temp = temp.next;
@@ -63,24 +58,21 @@ public void display() {
6358

6459
/**
6560
* Deletes the first occurrence of the specified element in the sorted linked list.
66-
*
6761
* @param value the value to be deleted
6862
* @return true if the element is found and deleted, false otherwise
6963
*/
70-
public boolean delete(int value){
64+
public boolean delete(int value) {
7165
if (this.head == null) {
7266
return false;
73-
}
74-
else if (this.head.value == value) {
67+
} else if (this.head.value == value) {
7568
if (this.head.next == null) {
7669
this.head = null;
7770
this.tail = null;
7871
} else {
7972
this.head = this.head.next;
8073
}
8174
return true;
82-
}
83-
else{
75+
} else{
8476
Node temp = this.head;
8577
while (temp.next != null) {
8678
if (temp.next.value == value) {
@@ -93,13 +85,11 @@ else if (this.head.value == value) {
9385
temp = temp.next;
9486
}
9587
return false;
96-
9788
}
9889
}
9990

10091
/**
10192
* Searches for the specified element in the sorted linked list.
102-
*
10393
* @param value the value to be searched
10494
* @return true if the element is found, false otherwise
10595
*/
@@ -116,7 +106,6 @@ public boolean search(int value) {
116106

117107
/**
118108
* Checks if the sorted linked list is empty.
119-
*
120109
* @return true if the list is empty, false otherwise
121110
*/
122111
public boolean isEmpty() {
@@ -125,7 +114,6 @@ public boolean isEmpty() {
125114

126115
/**
127116
* Returns the minimum value in the sorted linked list.
128-
*
129117
* @return the minimum value
130118
*/
131119
public int minValue() {
@@ -134,7 +122,6 @@ public int minValue() {
134122

135123
/**
136124
* Returns the maximum value in the sorted linked list.
137-
*
138125
* @return the maximum value
139126
*/
140127
public int maxValue() {
@@ -143,21 +130,19 @@ public int maxValue() {
143130

144131
/**
145132
* Returns a string representation of the sorted linked list.
146-
*
147133
* @return a string representation of the sorted linked list
148134
*/
149135
@Override
150136
public String toString() {
151137
if (this.head != null) {
152-
ArrayList<String> elements=new ArrayList<>();
138+
ArrayList<String> elements = new ArrayList<>();
153139
Node temp = this.head;
154140
while (temp != null) {
155141
elements.add(String.valueOf(temp.value));
156142
temp = temp.next;
157143
}
158144
return String.join(", ", elements);
159-
}
160-
else {
145+
} else {
161146
return "";
162147
}
163148
}

0 commit comments

Comments
 (0)