Skip to content

Commit 5b4d579

Browse files
committed
Check Palindrom in a Linked List and Add 2 numbers in Linked List
1 parent ffc508d commit 5b4d579

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

Linked_List/Add_2_Numbers.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
using namespace std;
3+
struct Node {
4+
int data;
5+
Node* next;
6+
Node(int val) : data(val), next(nullptr) {}
7+
};
8+
Node* reverse(Node* head) {
9+
Node* prev = nullptr;
10+
Node* current = head;
11+
Node* next = nullptr;
12+
while (current != nullptr) {
13+
next = current->next;
14+
current->next = prev;
15+
prev = current;
16+
current = next;
17+
}
18+
return prev;
19+
}
20+
void insertAtTail(Node*& head, Node*& tail, int val) {
21+
Node* temp = new Node(val);
22+
if (head == nullptr) {
23+
head = temp;
24+
tail = temp;
25+
} else {
26+
tail->next = temp;
27+
tail = temp;
28+
}
29+
}
30+
Node* addTwoNumbers(Node* first, Node* second) {
31+
int carry = 0;
32+
Node* ansHead = nullptr;
33+
Node* ansTail = nullptr;
34+
while (first != nullptr || second != nullptr || carry != 0) {
35+
int val1 = (first != nullptr) ? first->data : 0;
36+
int val2 = (second != nullptr) ? second->data : 0;
37+
int sum = carry + val1 + val2;
38+
int digit = sum % 10;
39+
carry = sum / 10;
40+
insertAtTail(ansHead, ansTail, digit);
41+
if (first != nullptr) first = first->next;
42+
if (second != nullptr) second = second->next;
43+
}
44+
return ansHead;
45+
}
46+
Node* addTwoLists(Node* first, Node* second) {
47+
first = reverse(first);
48+
second = reverse(second);
49+
Node* ans = addTwoNumbers(first, second);
50+
ans = reverse(ans);
51+
return ans;
52+
}
53+
// Helper function to print list
54+
void printList(Node* head) {
55+
while (head != nullptr) {
56+
cout << head->data << " ";
57+
head = head->next;
58+
}
59+
cout << endl;
60+
}
61+
// Example usage
62+
int main() {
63+
Node* first = nullptr;
64+
Node* tail1 = nullptr;
65+
insertAtTail(first, tail1, 4);
66+
insertAtTail(first, tail1, 5); // Represents 45
67+
Node* second = nullptr;
68+
Node* tail2 = nullptr;
69+
insertAtTail(second, tail2, 3);
70+
insertAtTail(second, tail2, 4); // Represents 34
71+
Node* result = addTwoLists(first, second);
72+
printList(result); // Should print 7 9 for 79
73+
return 0;
74+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
struct Node{
5+
int data;
6+
Node * next;
7+
Node(int val) : data(val), next(nullptr) {}
8+
};
9+
bool checkPalindrome(vector<int>& arr){
10+
int n = arr.size();
11+
int start = 0;
12+
int end = n - 1;
13+
while(start <= end){
14+
if(arr[start] != arr[end]){
15+
return false;
16+
}
17+
start++;
18+
end--;
19+
}
20+
return true;
21+
}
22+
bool isPalindrome(Node * head){
23+
vector<int> arr;
24+
Node * temp = head;
25+
while(temp != NULL){
26+
arr.push_back(temp->data);
27+
temp = temp->next;
28+
}
29+
return checkPalindrome(arr);
30+
}
31+
void printList(Node * head){
32+
Node * current = head;
33+
while(current != NULL){
34+
cout << current->data ;
35+
current = current->next;
36+
}
37+
cout << endl;
38+
}
39+
int main(){
40+
Node * head = new Node(1);
41+
head->next = new Node(2);
42+
head->next->next = new Node(3);
43+
head->next->next->next = new Node(3);
44+
head->next->next->next->next = new Node(2);
45+
head->next->next->next->next->next = new Node(1);
46+
if(isPalindrome(head)){
47+
cout << "The Given Linked List is a Palindrome. " << endl;
48+
}
49+
else{
50+
cout << "The Given Linked List is not a Palindrome. " << endl;
51+
}
52+
return 0;
53+
}

0 commit comments

Comments
 (0)