-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLL.java
More file actions
101 lines (98 loc) · 1.95 KB
/
SLL.java
File metadata and controls
101 lines (98 loc) · 1.95 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.*;
public class SLL
{
static Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public static SLL insert(SLL l,int d)
{
Node n = new Node(d);
if(l.head == null)
{
head = n;
return l;
}
else
{
Node temp = l.head;
while(temp.next!=null)
{
temp = temp.next;
}
temp.next = n;
}
return l;
}
public static void display(SLL l)
{
System.out.println("\nLL:");
Node nn = l.head;
while(nn!=null)
{
System.out.print(nn.data+" ");
nn=nn.next;
}
}
public static void reverse(SLL l)
{
Node prev = null;
Node temp = head;
Node curr = temp;
do
{
temp = temp.next;
curr.next = prev;
prev = curr;
curr = temp;
}while(temp.next!=null);
temp.next = prev;
head = temp;
}
public static void delet(int d)
{
Node temp=head;
if(temp.data==d)
{
head = head.next;
return;
}
Node b = null;
while(temp.data!=d)
{
b = temp;
temp=temp.next;
}
if(b.next.next == null)
{
b.next = null;
return;
}
temp.next = temp.next.next;
}
public static void main(String []pavan)
{
SLL l = new SLL();
l.insert(l,10);
l.insert(l,20);
l.insert(l,30);
l.insert(l,40);
l.insert(l,50);
display(l);
// delet(30);
// display(l);
// delet(10);
// display(l);
// delet(50);
// display(l);
reverse(l);
display(l);
}
}