Skip to content

Commit 4a2f3a4

Browse files
committed
Day-26: More Bit manipulation problems
1 parent 3b3f188 commit 4a2f3a4

File tree

4 files changed

+259
-4
lines changed

4 files changed

+259
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
| Current Status| Stats |
66
| :------------: | :----------: |
7-
| Total Problems | 40 |
8-
| Current Streak | 28 |
9-
| Longest Streak | 28 ( August 17, 2015 - September 13, 2015 ) |
7+
| Total Problems | 43 |
8+
| Current Streak | 29 |
9+
| Longest Streak | 29 ( August 17, 2015 - September 14, 2015 ) |
1010

1111
</center>
1212

@@ -22,7 +22,9 @@
2222
| Print middle node of linkedlist without iterating twice. | [printMiddleNode.cpp](linked_list_problems/printMiddleNode.cpp) | | Detecting and removing a cycle in linkedlist.| [floyedCycleDetection.cpp](linked_list_problems/floyedCycleDetection.cpp)|
2323
| Determine if a linked list is a pallindrome. | [listPallindrome.cpp](linked_list_problems/listPallindrome.cpp) |
2424
| Insert data in a sorted linked list.|[insertInASortedLinkedList.cpp](linked_list_problems/insertInASortedLinkedList.cpp) |
25-
25+
| Determine the intersection(merging) point of two given linked list.| [findIntersectionPointOfLists.cpp](linked_list_problems/findIntersectionPointOfLists.cpp)|
26+
| Clone a linkedlist which has next and an random pointer, Space Complexity - O(1). | [cloneListWithRandomPtr.cpp](linked_list_problems/cloneListWithRandomPtr.cpp)|
27+
| Given a sorted linked list with duplicates, remove duplicates in one iteration. | [removeDuplicatesFromSortedList.cpp](linked_list_problems/removeDuplicatesFromSortedList.cpp)|
2628

2729
### Include
2830
Include contains single header implementation of data structures and some algorithms.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <iostream>
2+
3+
struct Node {
4+
int data;
5+
Node * next;
6+
Node * random;
7+
Node( int d ) : data{ d }, next{ nullptr }, random{ nullptr } { }
8+
};
9+
10+
void printList( Node * head )
11+
{
12+
std::cout << "List in format (Node data, Random Node data) : \n";
13+
while( head ) {
14+
std::cout << "(" << head->data << ", " << head->random->data << ")-->";
15+
head = head->next;
16+
}
17+
std::cout << "NULL" << std::endl;
18+
}
19+
20+
Node * clone ( Node * head )
21+
{
22+
if ( head == nullptr || ((head != nullptr) && (head->next == nullptr))){
23+
return head;
24+
}
25+
Node * newNode = nullptr;
26+
Node * nextNode = nullptr;
27+
Node * currNode = head;
28+
/**
29+
* Adding a new node after each node with same data
30+
* 1->2->3 becomes 1->1->2->2->3->3
31+
*/
32+
while( currNode ) {
33+
nextNode = currNode->next;
34+
newNode = new Node(currNode->data);
35+
newNode->next = currNode->next;
36+
currNode->next = newNode;
37+
currNode = nextNode;
38+
}
39+
40+
//Now copying random pointer to all newly added node.
41+
currNode = head;
42+
while( currNode && (currNode->next != nullptr) ) {
43+
currNode->next->random = currNode->random->next;
44+
currNode = currNode->next->next;
45+
}
46+
47+
//splitting the list back
48+
Node * list1ptr = head;
49+
Node * list2ptr = head->next;
50+
Node * cloneHead = head->next;
51+
while( list1ptr->next && list2ptr->next ) {
52+
list1ptr->next = list1ptr->next->next;
53+
list2ptr->next = list2ptr->next->next;
54+
list1ptr = list1ptr->next;
55+
list2ptr = list2ptr->next;
56+
}
57+
return cloneHead;
58+
}
59+
60+
bool verifyClone( Node * list1, Node * list2 )
61+
{
62+
while( list1 && list2 ) {
63+
if ( (list1->data != list2->data) ||
64+
(list1->random->data != list2->random->data) ) {
65+
return false;
66+
}
67+
list1 = list1->next;
68+
list2 = list2->next;
69+
}
70+
return true;
71+
}
72+
73+
74+
75+
int main()
76+
{
77+
Node * head = new Node(1);
78+
head->next = new Node(2);
79+
head->next->next = new Node(3);
80+
head->next->next->next = new Node(4);
81+
head->next->next->next->next = new Node(5);
82+
83+
head->random = head->next->next;
84+
head->next->random = head;
85+
head->next->next->random = head->next->next->next->next;
86+
head->next->next->next->random = head->next->next;
87+
head->next->next->next->next->random = head->next;
88+
89+
printList(head);
90+
Node *cloneHead = clone(head);
91+
std::cout << "Are list cloned (0 for no, 1 for yes): "
92+
<< verifyClone(head, cloneHead) << std::endl;
93+
printList(cloneHead);
94+
return 0;
95+
96+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Given two linked lists, they both merge at some point.
3+
* Find out the merging point
4+
*/
5+
6+
#include <iostream>
7+
8+
struct Node {
9+
int data;
10+
Node * next;
11+
Node( int d ) : data{ d }, next{ nullptr } { }
12+
};
13+
14+
void printList( Node * head )
15+
{
16+
while( head ) {
17+
std::cout << head->data << "-->";
18+
head = head->next;
19+
}
20+
std::cout << "NULL" << std::endl;
21+
}
22+
23+
int listLen( Node * head )
24+
{
25+
int count = 0;
26+
while( head ) {
27+
head = head->next;
28+
count++;
29+
}
30+
return count;
31+
}
32+
33+
Node * mergePoint( Node * head1, Node * head2 )
34+
{
35+
int len1 = listLen(head1);
36+
int len2 = listLen(head2);
37+
//figure out the bigger list ( and smaller )
38+
//ptr points to bigger list, let us move the difference
39+
//between the two.
40+
Node * ptr1 = ( len1 > len2 ) ? head1 : head2;
41+
Node * ptr2 = ( len1 > len2 ) ? head2 : head1;
42+
int i = 0;
43+
while ( i < abs(len1 - len2) && ptr1 ) {
44+
ptr1 = ptr1->next;
45+
++i;
46+
}
47+
//Now we have equal nodes to travel on both the nodes
48+
// traversing and comparing the pointers.
49+
50+
while( ptr1 && ptr2 ) {
51+
if ( ptr1 == ptr2 ) {
52+
return ptr1;
53+
}
54+
ptr1 = ptr1->next;
55+
ptr2 = ptr2->next;
56+
}
57+
return nullptr;
58+
}
59+
60+
61+
int main()
62+
{
63+
Node * list1 = new Node(3);
64+
list1->next = new Node(6);
65+
list1->next->next = new Node(9);
66+
list1->next->next->next = new Node(12);
67+
list1->next->next->next->next = new Node(15);
68+
list1->next->next->next->next->next = new Node(18);
69+
70+
Node * list2 = new Node(7);
71+
list2->next = new Node(10);
72+
list2->next->next = list1->next->next->next;
73+
74+
printList(list1);
75+
printList(list2);
76+
77+
Node * mergeNode = mergePoint( list1 , list2 );
78+
std::cout << "Merge Node of lists is :" << mergeNode->data << std::endl;
79+
return 0;
80+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Given a sorted linkedlist remove duplicates from it.
3+
* 5->5->5->7->7->9->11->13->13->15->15
4+
* 5->7->9->11->13->15
5+
*/
6+
7+
#include <iostream>
8+
9+
struct Node {
10+
int data;
11+
Node * next;
12+
Node( int d ) : data{ d }, next{ nullptr } { }
13+
};
14+
15+
void insert( Node * & head, int data )
16+
{
17+
Node * newNode = new Node(data);
18+
if ( head == nullptr ) {
19+
head = newNode;
20+
} else {
21+
Node * temp = head;
22+
while( temp->next != nullptr ) {
23+
temp = temp->next;
24+
}
25+
temp->next = newNode;
26+
}
27+
}
28+
29+
void printList( Node * head )
30+
{
31+
while( head ) {
32+
std::cout << head->data << "-->";
33+
head = head->next;
34+
}
35+
std::cout << "NULL" << std::endl;
36+
}
37+
38+
void removeDuplicates ( Node * head )
39+
{
40+
if ( !head || (head && !(head->next))) {
41+
return;
42+
}
43+
Node * currNode = head;
44+
Node * nextNode = nullptr;
45+
while( currNode ) {
46+
nextNode = currNode->next;
47+
while(nextNode && nextNode->data == currNode->data) {
48+
nextNode = nextNode->next;
49+
}
50+
currNode->next = nextNode;
51+
currNode = nextNode;
52+
}
53+
}
54+
55+
int main()
56+
{
57+
Node * head = nullptr;
58+
// 5->5->5->7->7->9->11->13->13->13->15->15
59+
insert(head, 5);
60+
insert(head, 5);
61+
insert(head, 5);
62+
insert(head, 7);
63+
insert(head, 7);
64+
insert(head, 9);
65+
insert(head, 11);
66+
insert(head, 13);
67+
insert(head, 13);
68+
insert(head, 13);
69+
insert(head, 13);
70+
insert(head, 15);
71+
insert(head, 15);
72+
printList(head);
73+
74+
removeDuplicates(head);
75+
printList(head);
76+
return 0;
77+
}

0 commit comments

Comments
 (0)