-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day-055.cpp
193 lines (182 loc) · 4.4 KB
/
Day-055.cpp
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include<iostream>
using namespace std;
class LinkedListNode
{
public:
int data;
LinkedListNode *next;
LinkedListNode(int d){
this->data = data;
this->next = NULL;
}
};
LinkedListNode<int> *reverseLinkedList(LinkedListNode<int> *head)
{
if(head==NULL || head->next == NULL){
return head;
}
LinkedListNode<int>* prev = NULL;
LinkedListNode<int>* curr = head;
LinkedListNode<int>* forward = NULL;
while(curr != NULL){
forward = curr -> next;
curr->next = prev;
prev = curr;
curr = forward;
}
return prev;
}
Node* kReverse(Node* head, int k) {
if(head == NULL){
return NULL;
}
//step 1: reverse first k nodes
Node* next = NULL;
Node* curr = head;
Node* prev = NULL;
int count = 0;
while(curr != NULL && count < k){
next = curr ->next;
curr -> next = prev;
prev = curr;
curr = next;
count++;
}
// Recursion dekhlega baki ka
if(next!=NULL){
head -> next = kReverse(next,k);
}
return prev;
}
Node* getMiddle(Node* head){
if(head == NULL || head -> next== NULL){
return head;
}
// 2 nodes
if(head -> next -> next == NULL){
return head -> next;
}
Node* slow = head;
Node* fast = head->next;
while(fast != NULL){
fast= fast->next;
if(fast != NULL){
fast= fast->next;
}
slow = slow ->next;
}
}
Node *findMiddle(Node *head) {
return getMiddle(head);
}
class Solution{
Node* getMid(Node* head){
Node*slow =head;
Node*fast = head->next;
while(fast!=NULL && fast->next!=NULL){
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
Node* reverse(Node* head ){
Node* curr = head;
Node* prev = NULL;
Node* next = NULL;
while(curr != NULL){
next = curr -> next;
curr -> next == prev;
prev = curr;
curr = next;
}
return prev;
}
public:
//Function to check whether the list is palindrome.
bool isPalindrome(Node *head)
{
if(head->next == NULL){
return true;
}
// step-1 find middle
Node* middle = getMid(head);
//step-2 reverse list after middle
Node* temp = middle ->next;
middle -> next = reverse(temp);
//step-3 compare both halfs
Node*head1 = head;
Node*head2 = middle->next;
while(head2 != NULL){
if(head1->data != head2->data){
return false;
}
head1 = head1->next;
head2 = head2->next;
}//step 4 re arranging the linked list(optional)
temp = middle->next;
middle ->next = reverse(temp);
return true;
}
};
class Solution
{
Node* reverse(Node* head ){
Node* curr = head;
Node* prev = NULL;
Node* next = NULL;
while(curr != NULL){
next = curr -> next;
curr -> next == prev;
prev = curr;
curr = next;
}
return prev;
}
void insertAtTail(struct Node* head,struct Node* &tail, int d){
if(tail == NULL){
Node* temp = new Node(d);
head = temp;
tail = temp;
}
Node* temp = new Node(d);
tail ->next = temp;
tail = temp;
}
struct Node* add(struct Node* first,struct Node* second){
int carry = 0;
Node* ansHead = NULL;
Node* ansTail = NULL;
while(first != NULL || second != NULL ||carry !=0){
int val1=0;
if(first!=NULL)
val1 = first->data;
int val2=0;
if(second != NULL)
val2= second->data;
int sum = carry+val1+val2;
int digit = sum%10;
insertAtTail(ansHead,ansTail,digit);
carry = sum/10;
if(first != NULL){
first = first->next;
}
if(second != NULL){
second = second ->next;
}
}
return ansHead;
}
public:
//Function to add two numbers represented by linked list.
struct Node* addTwoLists(struct Node* first, struct Node* second)
{
// step 1- reverse input ll
first = reverse(first);
second = reverse(second);
// step 2 add 2 LL
Node* ans = add(first,second);
//step 3
ans = reverse(ans);
return ans;
}
};