Skip to content

Commit 6d42608

Browse files
committed
newProblems
1 parent d2b4626 commit 6d42608

19 files changed

+1120
-18
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package linkedlist;
2+
3+
public class AppendLastNToStart {
4+
5+
public static int length(LinkedListNode<Integer>head){
6+
7+
LinkedListNode<Integer>temp=head;
8+
int count=0;
9+
while(temp!=null){
10+
11+
count++;
12+
temp=temp.next;
13+
}
14+
15+
return count;
16+
17+
}
18+
public static LinkedListNode<Integer> append(LinkedListNode<Integer> root, int n) {
19+
20+
21+
int len=length(root);
22+
23+
24+
LinkedListNode<Integer> temp=root;
25+
int count=0;
26+
while(count<(len-n-1)){
27+
28+
temp=temp.next;
29+
30+
31+
count++;
32+
33+
34+
}
35+
36+
LinkedListNode<Integer> small=temp.next;
37+
temp.next=null;
38+
temp=small;
39+
while(temp.next!=null){
40+
41+
temp=temp.next;
42+
43+
44+
}
45+
46+
temp.next=root;
47+
48+
root=small;
49+
50+
return root;
51+
52+
53+
54+
}
55+
56+
public static LinkedListNode<Integer> appendFastSlowApproach(LinkedListNode<Integer> head, int n) {
57+
58+
59+
if(n==0|| head==null){
60+
return head;
61+
}
62+
63+
LinkedListNode<Integer> fast=head;
64+
LinkedListNode<Integer>slow=head;
65+
LinkedListNode<Integer> init=head;
66+
for(int i=0;i<n;i++){
67+
fast=fast.next;
68+
}
69+
while(fast.next!=null){
70+
slow=slow.next;
71+
fast=fast.next;
72+
}
73+
74+
LinkedListNode<Integer> temp=slow.next;
75+
slow.next=null;
76+
fast.next=init;
77+
head=temp;
78+
79+
return head;
80+
81+
}
82+
}

src/linkedlist/DeleteNnodes.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package linkedlist;
2+
3+
public class DeleteNnodes {
4+
5+
public static LinkedListNode<Integer> skipMdeleteN(LinkedListNode<Integer> head, int M, int N) {
6+
7+
if(M==0|| head==null){
8+
9+
return null;
10+
}
11+
12+
if(N==0){
13+
return head;
14+
}
15+
16+
LinkedListNode<Integer> temp=head;
17+
LinkedListNode<Integer>curr;
18+
19+
int i=1;
20+
while(temp!=null){
21+
22+
23+
for( i=1;i<M && temp!=null;i++){
24+
25+
temp=temp.next;
26+
27+
}
28+
29+
if(temp==null){
30+
return head;
31+
}
32+
33+
curr=temp.next;
34+
35+
for(i=1;i<=N && curr!=null;i++){
36+
37+
curr=curr.next;
38+
39+
}
40+
41+
temp.next=curr;
42+
temp=curr;
43+
}
44+
return head;
45+
46+
}
47+
48+
49+
public static LinkedListNode<Integer> skipMdeleteN2(LinkedListNode<Integer> head, int M, int N) {
50+
51+
if(M==0|| head==null){
52+
53+
return null;
54+
}
55+
56+
if(N==0){
57+
return head;
58+
}
59+
60+
LinkedListNode<Integer> curr=head;
61+
LinkedListNode<Integer>temp=null;
62+
63+
64+
while(curr!=null){
65+
66+
int yes=0;
67+
int no=0;
68+
while(curr!=null && yes<M){
69+
70+
if(temp==null){
71+
temp=curr;
72+
}
73+
else{
74+
75+
temp.next=curr;
76+
temp=curr;
77+
}
78+
79+
curr=curr.next;
80+
yes+=1;
81+
82+
}
83+
84+
while(curr!=null && no<N){
85+
86+
curr=curr.next;
87+
no+=1;
88+
}
89+
90+
}
91+
92+
if(temp!=null){
93+
temp.next=null;
94+
}
95+
return head;
96+
97+
}
98+
}

src/linkedlist/EvenOddLL.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package linkedlist;
2+
3+
public class EvenOddLL {
4+
5+
6+
public static LinkedListNode<Integer> sortEvenOdd(LinkedListNode<Integer> head) {
7+
8+
if(head==null){
9+
return head;
10+
11+
}
12+
13+
LinkedListNode<Integer>oh=null;
14+
LinkedListNode<Integer>ot=null;
15+
LinkedListNode<Integer>eh=null;
16+
LinkedListNode<Integer>et=null;
17+
18+
while(head!=null){
19+
20+
21+
if((head.data%2)!=0){
22+
23+
24+
if(oh==null){
25+
26+
oh=head;
27+
ot=head;
28+
29+
30+
}
31+
else{
32+
33+
ot.next=head;
34+
ot=ot.next;
35+
}
36+
}
37+
else{
38+
39+
if(eh==null){
40+
41+
eh=head;
42+
et=head;
43+
44+
}
45+
else{
46+
47+
et.next=head;
48+
et=et.next;
49+
50+
}
51+
52+
53+
54+
55+
}
56+
57+
head=head.next;
58+
}
59+
60+
61+
if(oh==null){
62+
et.next=null;
63+
return eh;
64+
}
65+
66+
if(eh==null){
67+
68+
ot.next=null;
69+
return oh;
70+
}
71+
72+
ot.next=eh;
73+
et.next=null;
74+
return oh;
75+
// if(oh!=null){
76+
// ot.next=null;
77+
// }
78+
// if(eh!=null){
79+
// et.next=null;
80+
// }
81+
82+
// if(oh==null){
83+
// return eh;
84+
// }
85+
86+
// if(eh==null){
87+
// return oh;
88+
// }
89+
// else{
90+
// ot.next=eh;
91+
// }
92+
93+
// return oh;
94+
//write your code here
95+
//
96+
//if(oddehead==null){
97+
// return evenhead;
98+
//}
99+
//else{
100+
// oddtail.next=evenhead;
101+
//}
102+
//if(evenhead!=null){
103+
// eventail.next=null;
104+
//}
105+
//
106+
//return oddhead
107+
108+
109+
}
110+
}

src/linkedlist/FindNode.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package linkedlist;
2+
3+
public class FindNode {
4+
public static int helper(LinkedListNode<Integer>head,int n){
5+
6+
7+
if(head==null){
8+
return -1;
9+
}
10+
11+
if(head.data==n){
12+
13+
return 0;
14+
}
15+
16+
int small= helper(head.next,n);
17+
if(small==-1){
18+
return -1;
19+
}
20+
return small+1;
21+
22+
}
23+
24+
}

src/linkedlist/SortLL.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SortLL {
1818

1919

2020

21-
public class Solution {
21+
2222

2323
public static DoublyLinkedListNode<Integer> merge(DoublyLinkedListNode<Integer>head1,DoublyLinkedListNode<Integer>head2){
2424

@@ -155,4 +155,4 @@ public static DoublyLinkedListNode<Integer> sorting(DoublyLinkedListNode<Integer
155155
}
156156

157157
}
158-
}
158+

src/linkedlist/reverseLLByK.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package linkedlist;
2+
3+
public class reverseLLByK {
4+
5+
6+
public static LinkedListNode<Integer> kReverseByRecursion(LinkedListNode<Integer> head, int k) {
7+
8+
9+
if(k==0 || k==1){
10+
return head;
11+
}
12+
13+
LinkedListNode<Integer> temp=head;
14+
LinkedListNode<Integer> prev=null;
15+
LinkedListNode<Integer> fwd=null;
16+
if(head==null){
17+
return head;
18+
}
19+
20+
int count=0;
21+
while(temp!=null && count<k){
22+
23+
fwd=temp.next;
24+
temp.next=prev;
25+
prev=temp;
26+
temp=fwd;
27+
count++;
28+
}
29+
30+
if(fwd!=null){
31+
head.next=kReverseByRecursion(fwd,k);
32+
}
33+
34+
35+
return prev;
36+
37+
}
38+
39+
}

0 commit comments

Comments
 (0)