-
Notifications
You must be signed in to change notification settings - Fork 53
/
LinkedListSearch.java
59 lines (46 loc) · 1.37 KB
/
LinkedListSearch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Java_DSA.Problems.Arrays;
// A Linked List Node
class Node {
int data;
Node next;
// Constructor to initialize a new node with data
Node(int new_data) {
data = new_data;
next = null;
}
}
public class LinkedListSearch {
// Checks whether key is present in linked list
static boolean searchKey(Node head, int key) {
// Initialize curr with the head of linked list
Node curr = head;
// Iterate over all the nodes
while (curr != null) {
// If the current node's value is equal to key,
// return true
if (curr.data == key)
return true;
// Move to the next node
curr = curr.next;
}
// If there is no node with value as key, return
// false
return false;
}
// Driver code
public static void main(String[] args) {
// Create a hard-coded linked list:
// 14 -> 21 -> 13 -> 30 -> 10
Node head = new Node(14);
head.next = new Node(21);
head.next.next = new Node(13);
head.next.next.next = new Node(30);
head.next.next.next.next = new Node(10);
// Key to search in the linked list
int key = 14;
if (searchKey(head, key))
System.out.println("Yes");
else
System.out.println("No");
}
}