-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLL.java
More file actions
115 lines (109 loc) · 1.95 KB
/
Copy pathLL.java
File metadata and controls
115 lines (109 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.*;
class Node
{
int data;
Node next=null;
}
class l{
Node head;
public void insert(int d)
{
Node node=new Node();
node.data=d;
if(head==null)
{
head=node;
}
else
{
Node n=head;
while(n.next!=null)
{
n=n.next;
}
n.next=node;
}
}
public void i(int dx)
{
Node node=new Node();
node.data=dx;
node.next=head;
head=node;
}
public void ias(int i,int x)
{
Node node=new Node();
node.data=x;
Node n=head;
for(int ix=0;ix<i-1;ix++)
{
n=n.next;
}
node.next=n.next;
n.next=node;
}
public void deleteNode(int ix)
{
if (head == null)
return;
Node temp = head;
if (ix == 0)
{
head = temp.next; // Change head
return;
}
for (int i=0; temp!=null && i<ix-1; i++)
temp = temp.next;
if (temp == null || temp.next == null)
return;
Node next = temp.next.next;
temp.next = next; // Unlink the deleted node from list
}
public void reverse() {
Node p1 = null;
Node p2 = head;
Node p3 = null;
while (p2 != null) {
p3 = p2.next;
p2.next = p1;
p1 = p2;
p2 = p3;
}
head = p1;
}
public void display()
{
Node n=head;
while(n!=null)
{
System.out.print(n.data+" ");
n=n.next;
}
}
public static void main(String[] args) {
l a=new l();
Scanner s=new Scanner(System.in);
System.out.println("enter value");
int x=s.nextInt();
while(x>0)
{
a.insert(x);
System.out.println("enter value");
x=s.nextInt();
}
a.display();
a.i(45);
System.out.println();
a.display();
a.ias(2,55);
System.out.println();
a.display();
a.deleteNode(2);
System.out.println();
a.display();
a.reverse();
System.out.println();
a.display();
}
}