mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-13 07:13:37 +08:00
search in singly linked list using recursion
This commit is contained in:
32
DataStructures/Lists/SearchSinglyLinkedListRecursion.java
Normal file
32
DataStructures/Lists/SearchSinglyLinkedListRecursion.java
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package DataStructures.Lists;
|
||||||
|
|
||||||
|
public class SearchSinglyLinkedListRecursion extends SinglyLinkedList {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SearchSinglyLinkedListRecursion list = new SearchSinglyLinkedListRecursion();
|
||||||
|
for (int i = 1; i <= 10; ++i) {
|
||||||
|
list.insert(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i <= 10; ++i) {
|
||||||
|
assert list.search(i);
|
||||||
|
}
|
||||||
|
assert !list.search(-1)
|
||||||
|
&& !list.search(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the value key is present in the list using recursion.
|
||||||
|
*
|
||||||
|
* @param node the head node.
|
||||||
|
* @param key the value to be searched.
|
||||||
|
* @return {@code true} if key is present in the list, otherwise {@code false}.
|
||||||
|
*/
|
||||||
|
private boolean searchRecursion(Node node, int key) {
|
||||||
|
return node != null && (node.value == key || searchRecursion(node.next, key));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean search(int key) {
|
||||||
|
return searchRecursion(getHead(), key);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user