-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
54 lines (46 loc) · 1.22 KB
/
Copy pathLinkedList.java
File metadata and controls
54 lines (46 loc) · 1.22 KB
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
public class LinkedList{
public static class Node{
int data;
Node next;
public Node(int data){
this.data = data;
this.next = null;
}
}
public static Node head;
public static Node tail;
public static int size;
public static int search(int key){
Node temp = head;
int i = 0;
while(temp != null){
if(temp.data == key){
return i;
}
temp = temp.next;
i++;
}
return -1;
}
public static void main(String args[]){
//create nodes..
head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
tail = new Node(4);
//link the nodes..
head.next = second;
second.next = third;
third.next = tail;
tail.next = null;
//print the list..
Node temp = head;
while(temp != null){
System.out.print(temp.data + "->");
temp = temp.next;
}
System.out.println("null");
int key = 3;
System.out.println("Element found at index: "+search(key));
}
}