Skip to content

Commit

Permalink
add khushal87 linked-list-code
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed Feb 22, 2021
1 parent 2cf2165 commit e06cfe0
Show file tree
Hide file tree
Showing 45 changed files with 545 additions and 96 deletions.
16 changes: 0 additions & 16 deletions .vscode/c_cpp_properties.json

This file was deleted.

29 changes: 0 additions & 29 deletions .vscode/launch.json

This file was deleted.

18 changes: 0 additions & 18 deletions .vscode/settings.json

This file was deleted.

27 changes: 0 additions & 27 deletions .vscode/tasks.json

This file was deleted.

Empty file modified Bit Manipulation/check_if_number_is_power_of_2
100755 → 100644
Empty file.
Empty file modified Bit Manipulation/copy_set_bits_in_range
100755 → 100644
Empty file.
Empty file modified Bit Manipulation/number_of_set_bits
100755 → 100644
Empty file.
Empty file modified Bit Manipulation/number_with_kth_bit_set
100755 → 100644
Empty file.
Empty file modified Bit Manipulation/power_set_string
100755 → 100644
Empty file.
Empty file modified Bit Manipulation/set_bit_position
100755 → 100644
Empty file.
Empty file modified Disjoint Set/teacher_dilemma
100755 → 100644
Empty file.
Empty file modified Disjoint Set/union_compression
100755 → 100644
Empty file.
Empty file modified Graph/Grid/dfs
100755 → 100644
Empty file.
Empty file modified Graph/Grid/number_of_connected_components
100755 → 100644
Empty file.
Binary file removed Graph/High Level/bellmanford
Binary file not shown.
Binary file removed Graph/High Level/djisktra
Binary file not shown.
Binary file removed Graph/bipartite_or_not
Binary file not shown.
Binary file removed Graph/breadth_first_search
Binary file not shown.
Binary file removed Graph/diameter_of_a_tree
Binary file not shown.
Binary file removed Graph/given_graph_is_tree_or_not
Binary file not shown.
Binary file removed Graph/graph_has_cycle_or_not
Binary file not shown.
Binary file removed Graph/in_out_time_of_binary_tree
Binary file not shown.
Binary file removed Graph/mininum_distance_between_two_nodes
Binary file not shown.
Binary file removed Graph/number_of_connected_components
Binary file not shown.
Binary file removed Graph/subtree_size_of_tree
Binary file not shown.
Binary file removed Graph/topological_sort
Binary file not shown.
49 changes: 49 additions & 0 deletions LinkedList/add_two_numbers.cpp
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;
}
57 changes: 57 additions & 0 deletions LinkedList/clockwise_rotate_linked_list.cpp
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;
}
}
17 changes: 17 additions & 0 deletions LinkedList/delete_node_in_linked_list.cpp
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;
}
34 changes: 34 additions & 0 deletions LinkedList/delete_nth_node_from_the_end.cpp
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;
}
7 changes: 2 additions & 5 deletions LinkedList/detect_loop.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <bits/stdc++.h>
#include <iostream>
#include <unordered_set>
using namespace std;

struct Node
Expand Down Expand Up @@ -40,7 +41,3 @@ bool detectLoop1(Node *head)
}
return false;
}

int main()
{
}
64 changes: 64 additions & 0 deletions LinkedList/find_intersection_in_two_linked_list.cpp
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);
}
}
22 changes: 22 additions & 0 deletions LinkedList/find_middle_in_linked_list.cpp
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;
}
Loading

0 comments on commit e06cfe0

Please sign in to comment.