-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
45 changed files
with
545 additions
and
96 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
/* data */ | ||
int data; | ||
Node *next; | ||
}; | ||
|
||
Node *newNode(int data) | ||
{ | ||
Node *newnode = new Node; | ||
newnode->data = data; | ||
newnode->next = nullptr; | ||
return newnode; | ||
} | ||
|
||
Node *addTwoList(Node *head1, Node *head2) | ||
{ | ||
Node *res = NULL; | ||
Node *temp, *prev = NULL; | ||
int carry = 0, sum; | ||
while (head1 != NULL || head2 != NULL) | ||
{ | ||
sum = carry + (head1 ? head1->data : 0) + (head2 ? head2->data : 0); | ||
carry = (sum >= 10) ? 1 : 0; | ||
sum %= 10; | ||
temp = newNode(sum); | ||
if (res == nullptr) | ||
{ | ||
res = temp; | ||
} | ||
else | ||
{ | ||
prev->next = temp; | ||
} | ||
prev = temp; | ||
if (head1) | ||
head1 = head1->next; | ||
if (head2) | ||
head2 = head2->next; | ||
} | ||
if (carry > 0) | ||
{ | ||
temp->next = newNode(carry); | ||
} | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
struct ListNode | ||
{ | ||
/* data */ | ||
int val; | ||
ListNode *next; | ||
}; | ||
|
||
int getCount(ListNode *head) | ||
{ | ||
int ctr = 0; | ||
ListNode *curr = head; | ||
while (curr != NULL) | ||
{ | ||
ctr++; | ||
curr = curr->next; | ||
} | ||
return ctr; | ||
} | ||
|
||
ListNode *rotateRight(ListNode *head, int k) | ||
{ | ||
if (k == 0 || head == nullptr) | ||
{ | ||
return head; | ||
} | ||
else | ||
{ | ||
int n = getCount(head); | ||
cout << n; | ||
if (k > n) | ||
{ | ||
k %= n; | ||
} | ||
k = n - k; | ||
if (k == 0) | ||
{ | ||
return head; | ||
} | ||
ListNode *curr = head; | ||
while (curr->next != nullptr) | ||
{ | ||
curr = curr->next; | ||
} | ||
curr->next = head; | ||
curr = head; | ||
for (int i = 0; i < k - 1; i++) | ||
{ | ||
curr = curr->next; | ||
} | ||
head = curr->next; | ||
curr->next = nullptr; | ||
return head; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
/* data */ | ||
int data; | ||
Node *next; | ||
}; | ||
|
||
void deleteNode(Node *node) | ||
{ | ||
Node *temp = node->next; | ||
node->data = temp->data; | ||
node->next = temp->next; | ||
delete temp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
/* data */ | ||
int data; | ||
Node *next; | ||
}; | ||
|
||
Node *deleteFunction(Node *head, int key) | ||
{ | ||
Node *first = head; | ||
Node *second = head; | ||
for (int i = 0; i < key; i++) | ||
{ | ||
if (second->next == NULL) | ||
{ | ||
if (i == key - 1) | ||
{ | ||
head = head->next; | ||
} | ||
return head; | ||
} | ||
second = second->next; | ||
} | ||
while (second->next != NULL) | ||
{ | ||
first = first->next; | ||
second = second->next; | ||
} | ||
first->next = first->next->next; | ||
return head; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
struct ListNode | ||
{ | ||
/* data */ | ||
int val; | ||
ListNode *next; | ||
}; | ||
|
||
int getCount(ListNode *root) | ||
{ | ||
ListNode *curr = root; | ||
int ctr = 0; | ||
while (curr != nullptr) | ||
{ | ||
ctr++; | ||
curr = curr->next; | ||
} | ||
return ctr; | ||
} | ||
|
||
ListNode *_getIntersectionNode(int d, ListNode *list1, ListNode *list2) | ||
{ | ||
ListNode *curr1 = list1; | ||
ListNode *curr2 = list2; | ||
for (int i = 0; i < d; i++) | ||
{ | ||
if (curr1 == NULL) | ||
{ | ||
return nullptr; | ||
} | ||
curr1 = curr1->next; | ||
} | ||
while (curr1 != nullptr && curr2 != nullptr) | ||
{ | ||
if (curr1 == curr2) | ||
return curr1; | ||
curr1 = curr1->next; | ||
curr2 = curr2->next; | ||
} | ||
return nullptr; | ||
} | ||
|
||
ListNode *getIntersectionNode(ListNode *list1, ListNode *list2) | ||
{ | ||
if (list1 == nullptr) | ||
return nullptr; | ||
if (list2 == nullptr) | ||
return nullptr; | ||
int ctr1 = getCount(list1); | ||
int ctr2 = getCount(list2); | ||
int d; | ||
if (ctr1 > ctr2) | ||
{ | ||
d = ctr1 - ctr2; | ||
return _getIntersectionNode(d, list1, list2); | ||
} | ||
else | ||
{ | ||
d = ctr2 - ctr1; | ||
return _getIntersectionNode(d, list2, list1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
struct Node | ||
{ | ||
/* data */ | ||
int data; | ||
Node *next; | ||
}; | ||
|
||
//fastest approach | ||
Node *middleElement(Node *head) | ||
{ | ||
Node *slow = head; | ||
Node *fast = head; | ||
while (fast && slow && fast->next) | ||
{ | ||
slow = slow->next; | ||
fast = fast->next->next; | ||
} | ||
return slow; | ||
} |
Oops, something went wrong.