33
33
LinkedList updLL1=new LinkedList();
34
34
updLL1.formRes(LL1,LL2,LL3,MID);
35
35
updLL1.display();
36
- updLL1.Size ();
36
+ updLL1.size ();
37
37
38
38
*/
39
39
40
-
41
-
42
40
import java .util .*;
43
41
import java .lang .*;
44
42
import java .io .*;
45
- class LinkedList {
43
+
44
+ public class LinkedList {
45
+
46
46
private class Node {
47
47
int data ;
48
48
Node next ;
@@ -52,9 +52,16 @@ private class Node{
52
52
this .next = null ;
53
53
}
54
54
}
55
- public Node head = null;
56
- public Node tail = null;
57
- private int size=0;
55
+
56
+ private Node head ;
57
+ private Node tail ;
58
+ private int size ;
59
+
60
+ public LinkedList () {
61
+ head = null ;
62
+ tail = null ;
63
+ size = 0 ;
64
+ }
58
65
59
66
public void addLast (int data ) {
60
67
Node newNode = new Node (data );
@@ -92,6 +99,7 @@ public void formLL2(LinkedList LL1) {
92
99
current =current .next .next ;
93
100
}
94
101
}
102
+
95
103
public void formLL3 (LinkedList LL1 ) {
96
104
Node current =LL1 .head .next ;
97
105
while (current .next !=null &¤t .next .next !=null ) {
@@ -100,6 +108,7 @@ public void formLL3(LinkedList LL1) {
100
108
current =current .next .next ;
101
109
}
102
110
}
111
+
103
112
public Node mid () {
104
113
Node slow =this .head ;
105
114
Node fast =this .head ;
@@ -109,11 +118,13 @@ public Node mid() {
109
118
}
110
119
return slow ;
111
120
}
121
+
112
122
public Node midValue () {
113
123
int sum =this .head .data +this .tail .data ;
114
124
Node mid =new Node (sum );
115
125
return mid ;
116
126
}
127
+
117
128
public void formRes (LinkedList LL1 ,LinkedList LL2 ,LinkedList LL3 ,Node MID ) {
118
129
Node LL1mid =LL1 .mid ();
119
130
Node currentLL1 =LL1 .head ;
@@ -135,7 +146,8 @@ else if(currentLL2==null&¤tLL3!=null) {
135
146
currentLL1 =currentLL1 .next ;
136
147
}
137
148
}
138
- public void Size() {
149
+
150
+ public void size () {
139
151
System .out .println (this .size );
140
152
}
141
153
}
0 commit comments