Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating a new folder having questions on linked list #447

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
68 changes: 68 additions & 0 deletions Linked List Questions/question1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};

int findNode(Node* head, int n) {
Node* temp = head;
for (int i = 0; temp != nullptr; i++) {
if (n == temp->data) {
return i;
}
temp = temp->next;
}
return -1;
}

void printList(Node* head) {
Node* current = head;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}

Node* createList(int arr[], int size) {
if (size == 0) return nullptr;
Node* head = new Node(arr[0]);
Node* current = head;
for (int i = 1; i < size; i++) {
current->next = new Node(arr[i]);
current = current->next;
}
return head;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
Node* head = createList(arr, 5);

cout << "Original List: ";
printList(head);

int n = 3;
int index = findNode(head, n);
if (index != -1) {
cout << "Element " << n << " found at index: " << index << endl;
} else {
cout << "Element " << n << " not found in the list." << endl;
}

Node* temp;
while (head != nullptr) {
temp = head;
head = head->next;
delete temp;
}

return 0;
}
Binary file added Linked List Questions/question1.exe
Binary file not shown.
84 changes: 84 additions & 0 deletions Linked List Questions/question2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Problem statement
// You have been given a singly linked list of integers along with an integer 'N'. Write a function to append the last 'N' nodes towards the front of the singly linked list and returns the new head to the list.

// Hint:
// Identify how many pointers you require and try traversing them to right places and connect nodes accordingly also don't forget to disconnect what's required else it could create cycles.

#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};

Node* appendLastNToFirst(Node* head, int n) {
if (head == nullptr) {
return head;
}
int count = 0;
Node* p1 = head;
Node* p2 = head;

while (p1->next != nullptr) {
count++;
p1 = p1->next;
}
p1->next = head;

for (int i = 0; i < count - n; i++) {
p2 = p2->next;
}

head = p2->next;
p2->next = nullptr;
return head;
}

void printList(Node* head) {
Node* current = head;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}

Node* createList(int arr[], int size) {
if (size == 0) return nullptr;
Node* head = new Node(arr[0]);
Node* current = head;
for (int i = 1; i < size; i++) {
current->next = new Node(arr[i]);
current = current->next;
}
return head;
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
Node* head = createList(arr, 5);

cout << "Original List: ";
printList(head);

int n = 3;
head = appendLastNToFirst(head, n);

cout << "List after appending last " << n << " nodes to the front: ";
printList(head);

Node* temp;
while (head != nullptr) {
temp = head;
head = head->next;
delete temp;
}

return 0;
}
Binary file added Linked List Questions/question2.exe
Binary file not shown.
77 changes: 77 additions & 0 deletions Linked List Questions/question3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Problem statement
// You have been given a singly linked list of integers where the elements are sorted in ascending order. Write a function that removes the consecutive duplicate values such that the given list only contains unique elements and returns the head to the updated list.
#include <iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};

Node* removeDuplicates(Node* head) {
if (head == nullptr) {
return head;
}
Node* pt1 = head;
Node* pt2 = head->next;

while (pt2 != nullptr) {
if (pt1->data != pt2->data) {
pt1 = pt1->next;
pt2 = pt2->next;
} else {
Node* temp = pt2;
pt2 = pt2->next;
pt1->next = pt2;
delete temp;
}
}
return head;
}

void printList(Node* head) {
Node* current = head;
while (current != nullptr) {
cout << current->data << " -> ";
current = current->next;
}
cout << "nullptr" << endl;
}

Node* createList(int arr[], int size) {
if (size == 0) return nullptr;
Node* head = new Node(arr[0]);
Node* current = head;
for (int i = 1; i < size; i++) {
current->next = new Node(arr[i]);
current = current->next;
}
return head;
}

int main() {
int arr[] = {1, 1, 2, 3, 3, 4, 4, 4, 5};
Node* head = createList(arr, 9);

cout << "Original List: ";
printList(head);

head = removeDuplicates(head);

cout << "List after removing duplicates: ";
printList(head);

Node* temp;
while (head != nullptr) {
temp = head;
head = head->next;
delete temp;
}

return 0;
}
Binary file added Linked List Questions/question3.exe
Binary file not shown.