-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList3.java
71 lines (61 loc) · 1.73 KB
/
LinkedList3.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
60
61
62
63
64
65
66
67
68
69
70
71
//REVERSE OPERATION ON LL AND SEARCH OPERATION ON LL
import java.util.Scanner;
class Node2{
int data;
Node2 next;
Node2(int val){
data=val;
next=null;
}
}
public class LinkedList3 {
static void printList(Node2 head){
while(head!=null){
System.out.print(head.data);
if(head.next!=null){
System.out.print(" -> ");
}
head=head.next;
}
System.out.println();
}
static Node2 reverseList(Node2 head){
Node2 curr=head;
Node2 prev=null;
Node2 next;
while(curr!=null){
next=curr.next;
curr.next=prev;
prev=curr;
curr=next;
}
return prev;
}
static boolean searchList(int a,Node2 temp){
Node2 curr=temp;
while(curr!=null){
if(curr.data==a)
return true;
curr=curr.next;
}
return false;
}
public static void main(String args[]) {
Scanner scan=new Scanner(System.in);
Node2 head = new Node2(10);
head.next = new Node2(20);
head.next.next = new Node2(30);
head.next.next.next=new Node2(40);
System.out.println("Given Linked List");
printList(head);
System.out.println("Reverse of given Linked List");
head= reverseList(head);
printList(head);
System.out.println("Enter value you want to search in this given Linked List");
int key=scan.nextInt();
if(searchList( key,head))
System.out.println("YES ,PRESENT");
else
System.out.println("NOT PRESENT");
}
}