-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
161 lines (159 loc) · 2.63 KB
/
SinglyLinkedList.java
File metadata and controls
161 lines (159 loc) · 2.63 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package day2.LinkedList;
public class SinglyLinkedList {
static class Node{
Object data;
Node next;
Node(Object data){
this.data=data;
}
}
Node head;
void add(Object d) {
Node n=new Node(d);
if(head==null) {
head=n;
return;
}
Node t=head;
while(t.next!=null)
t=t.next;
t.next=n;
}
void addFirst(Object d) {
Node n=new Node(d);
if(head==null) {
head=n;
return;
}
n.next=head;
head=n;
}
void addIndex(Object d,int i) {
if(i==0)
addFirst(d);
Node t=head, n=new Node(d);
while(i>1 && t!=null) {
t=t.next;
i--;
}
if(i>1 || t==null) {
System.out.println("Invalid Index");
return;
}
n.next=t.next;
t.next=n;
}
Object deleteFirst() {
if(head==null) {
System.out.println("List is Empty");
return null;
}
Object d=head.data;
head=head.next;
return d;
}
Object deleteLast() {
if(head==null) {
System.out.println("List is Empty");
return null;
}
Node t=head,pr=null;
while(t.next!=null) {
pr=t;
t=t.next;
}
if(pr!=null)
pr.next=null;
else
head=null;
return t.data;
}
Object deleteIndex(int i) {
if(i==0)
return deleteFirst();
Node t=head;
while(i>1 && t!=null) {
t=t.next;
i--;
}
if(i>1 || t==null || t.next==null) {
System.out.println("Invalid Index");
return null;
}
Object d=t.next.data;
t.next=t.next.next;
return d;
}
int getSum() {
int s=0;
Node t=head;
while(t!=null)
s+=(int)t.data;
return s;
}
boolean searchNode(Object n) {
Node t=head;
while(t!=null) {
if(t.data==n)
return true;
}
return false;
}
SinglyLinkedList mergeSLL(SinglyLinkedList f,SinglyLinkedList l) {
if(f.head==null && l.head!=null)
return l;
else if(l.head==null)
return f;
Node t=f.head;
while(t.next!=null)
t=t.next;
t.next=l.head;
return f;
}
void revLL() {
Node p=null,n=null,c=head;
while(c!=null) {
n=c.next;
c.next=p;
p=c;
c=n;
}
head=p;
}
SinglyLinkedList convertNTL(int a) {
SinglyLinkedList s=new SinglyLinkedList();
do {
s.addFirst(a%10);
a=a/10;
}while(a!=0);
return s;
}
SinglyLinkedList addNum(int a,int b) {
SinglyLinkedList p=convertNTL(a);
SinglyLinkedList q=convertNTL(b);
SinglyLinkedList r=new SinglyLinkedList();
Node t1=p.head;
Node t2=q.head;
int cr=0;
while(t1!=null && t2!=null) {
int sum=(int)t1.data+(int)t2.data+cr;
r.addFirst(sum%10);
cr=sum/10;
t1=t1.next;
t2=t2.next;
}
while(t1!=null) {
int sum=(int)t1.data+cr;
r.addFirst(sum%10);
cr=sum/10;
t1=t1.next;
}
while(t2!=null) {
int sum=(int)t2.data+cr;
r.addFirst(sum%10);
cr=sum/10;
t2=t2.next;
}
return r;
}
}