Problem:
Implement the delete() method of a Linked List
Output:
Not applicable.
Solution:
Node delete(Node start, int x) {
// precondition: the list is in ascending order;
// postconditions: the list is in ascending order, and if it did
// contains x, then the first occurrence of x has been deleted;
if (start == null || start.data > x) { // x is not in the list
return start;
} else if (start.data == x) { // x is the first element in the list
return start.next;
}
for (Node p = start; p.next != null; p = p.next) {
if (p.next.data > x) {
break; // x is not in the list
} else if (p.next.data == x) { // x is in the p.next node
p.next = p.next.next; // delete it
break;
}
}
return start;
}
No comments :
Post a Comment