Skip to content

Commit 7e7bb13

Browse files
committed
newProblems
1 parent 689d09e commit 7e7bb13

File tree

10 files changed

+866
-6
lines changed

10 files changed

+866
-6
lines changed

src/linkedlist/AdditionLL.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package linkedlist;
2+
3+
public class AdditionLL {
4+
5+
public static LinkedListNode<Integer> reverse(LinkedListNode<Integer> head) {
6+
if (head == null || head.next == null)
7+
return head;
8+
9+
LinkedListNode<Integer> prev = null;
10+
LinkedListNode<Integer> curr = head;
11+
while (curr != null) {
12+
LinkedListNode<Integer> temp = curr.next;
13+
curr.next = prev;
14+
prev = curr;
15+
curr = temp;
16+
}
17+
head = prev;
18+
return head;
19+
}
20+
21+
public static LinkedListNode<Integer> addNumbers(LinkedListNode<Integer> l1, LinkedListNode<Integer> l2) {
22+
23+
if(l2 == null) return l1;
24+
if(l1 == null) return l2;
25+
26+
27+
l1 = reverse(l1);
28+
29+
30+
l2 = reverse(l2);
31+
32+
LinkedListNode<Integer> head = l1;
33+
LinkedListNode<Integer> prev = null;
34+
int c = 0,sum;
35+
while(l1 != null && l2 != null)
36+
{
37+
sum = c + l1.data + l2.data;
38+
l1.data = sum % 10;
39+
c = sum / 10;
40+
prev = l1;
41+
l1 = l1.next;
42+
l2 = l2.next;
43+
}
44+
45+
if(l1 != null||l2 != null)
46+
{
47+
if(l2 != null) prev.next = l2;
48+
l1 = prev.next;
49+
while(l1 != null)
50+
{
51+
sum = c + l1.data;
52+
l1.data = sum % 10;
53+
c = sum / 10;
54+
prev = l1;
55+
l1 = l1.next;
56+
}
57+
}
58+
if(c > 0) prev.next = new LinkedListNode<Integer>(c);
59+
return reverse(head);
60+
61+
}
62+
63+
}

src/linkedlist/CloneLL.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package linkedlist;
2+
3+
import java.util.*;
4+
5+
class Node<T> {
6+
T data;
7+
Node<T> next;
8+
Node<T> arb;
9+
Node<T> down;
10+
Node(T data) {
11+
this.data = data;
12+
this.next = null;
13+
this.arb = null;
14+
this.down=null;
15+
}
16+
}
17+
18+
19+
20+
public class CloneLL {
21+
22+
public static Node<Integer> clone(Node<Integer> head) {
23+
24+
if(head==null ){
25+
return head;
26+
}
27+
28+
Map<Node<Integer>,Node<Integer> >mp=new HashMap<>();
29+
30+
Node<Integer>curr=head;
31+
Node<Integer>clone=null;
32+
while(curr!=null){
33+
34+
clone=new Node<Integer>(curr.data);
35+
mp.put(curr,clone);
36+
37+
curr=curr.next;
38+
}
39+
40+
41+
curr=head;
42+
43+
while(curr!=null){
44+
45+
46+
clone=mp.get(curr);
47+
clone.next=mp.get(curr.next);
48+
clone.arb=mp.get(curr.arb);
49+
curr=curr.next;
50+
51+
52+
53+
}
54+
55+
return mp.get(head);
56+
57+
}
58+
59+
public static Node<Integer> cloneLLAnotherApproach(Node<Integer> head){
60+
61+
if(head==null){
62+
return null;
63+
}
64+
Node<Integer>current=head;
65+
66+
// add cloned nodes
67+
while(current!=null){
68+
Node<Integer> temp=current.next;
69+
Node<Integer> newNode=new Node<>(current.data);
70+
current.next=newNode;
71+
newNode.next=temp;
72+
current=temp;
73+
74+
}
75+
76+
current=head;
77+
// adding arb pointer...
78+
while(current!=null){
79+
80+
if(current.next!=null){
81+
if(current.arb!=null){
82+
current.next.arb=current.arb.next;
83+
}
84+
else{
85+
//current.arb equals NULL..
86+
current.next.arb=current.arb;
87+
}
88+
}
89+
if(current.next!=null){
90+
current=current.next.next;
91+
}
92+
else{
93+
current=current.next;
94+
}
95+
}
96+
97+
// separate two LL..
98+
Node<Integer> origin=head;
99+
Node<Integer> cp=head.next;
100+
Node<Integer> temp=cp;
101+
while(origin!=null && cp!=null){
102+
103+
if(origin.next!=null){
104+
105+
origin.next=origin.next.next;
106+
}
107+
if(cp.next!=null){
108+
cp.next=cp.next.next;
109+
}
110+
111+
origin=origin.next;
112+
cp=cp.next;
113+
}
114+
115+
return temp;
116+
}
117+
}

0 commit comments

Comments
 (0)