-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyLinkedList.java
More file actions
127 lines (122 loc) · 3 KB
/
MyLinkedList.java
File metadata and controls
127 lines (122 loc) · 3 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
package fastds;
import java.util.Collections;
import java.util.LinkedList;
class Node{
int data;
Node next;
public Node(int d){
this.data=d;
this.next=null;
}
}
public class MyLinkedList implements MyUtils {
public Node head;
public int COUNT=0;
MyLinkedList(){
head=null;
}
@Override
public void reverse(){
Node trav=head,prev=null,nextNode;
while(trav!=null){
nextNode=trav.next;
trav.next=prev;
prev=trav;
trav=nextNode;
}
head=prev;
}
public void addLast(int e){
++COUNT;
Node node=new Node(e);
if(head==null)
head=node;
else{
Node trav=head;
while(trav.next!=null)
trav=trav.next;
trav.next=node;
}
}
public void addFirst(int e){
++COUNT;
Node node=new Node(e);
node.next=head;
head=node;
}
public int get(int d){
Node trav=head;
int index=0;
while(trav.next!=null){
if(trav.data==d)
return index;
index++;
trav=trav.next;
}
return index;
}
@Override
public void sort(){
}
@Override
public void print(){
Node trav=head;
while(trav!=null){
System.out.print(" "+trav.data);
trav=trav.next;
}
}
}
class ListAdd{
public static void listAdd(Node headA,Node headB,int lenA,int lenB){
LinkedList<Integer> list=new LinkedList<>();
Node travA,travB;
travA=headA;travB=headB;
int SUM=0,CARRY=0,REM;
while(travA!=null && travB!=null){
SUM=(travA.data + travB.data)+CARRY;
if(SUM > 9){
REM=SUM%10;
CARRY=SUM/10;
list.add(REM);
}else{
list.add(SUM);
CARRY=0;
}
travA=travA.next;
travB=travB.next;
}
if(lenA==lenB){
if(CARRY > 0){
list.add(CARRY);
}
}
while(travA!=null){
SUM=(travA.data)+CARRY;
if(SUM > 9){
REM=SUM%10;
CARRY=SUM/10;
list.add(REM);
}else{
list.add(SUM);
CARRY=0;
}
travA=travA.next;
}
while(travB!=null){
SUM=(travB.data)+CARRY;
if(SUM > 9){
REM=SUM%10;
CARRY=SUM/10;
list.add(REM);
}else{
list.add(SUM);
CARRY=0;
}
travB=travB.next;
}
Collections.reverse(list);
for(int x:list)
System.out.print(" "+x);
}
}