Skip to content

Commit 483cb8d

Browse files
committed
8/11 to 8/13 (Driving with JJ to MiW in Huntsville and flight back)
- sorted_lists_merge.cc - delete_node_from_list.cc - is_list_cyclic.cc - reverse_sublist.cc
1 parent a2ec864 commit 483cb8d

File tree

5 files changed

+151
-10
lines changed

5 files changed

+151
-10
lines changed

elements-of-programming-interviews/cpp/delete_node_from_list.cc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@
33
#include "test_framework/timed_executor.h"
44

55
// Assumes node_to_delete is not tail.
6+
/*
7+
Linear time: shift node->next's data to node. Then remove the final node
8+
from the list.
9+
10+
while (node && node->next && node->next->next){
11+
node->data = node->next->data
12+
node = node->next;
13+
}
14+
node->data = node->next->data
15+
node->next = nullptr;
16+
*/
17+
618
void DeletionFromList(const shared_ptr<ListNode<int>>& node_to_delete) {
7-
// TODO - you fill in here.
19+
shared_ptr<ListNode<int>> node = node_to_delete;
20+
while (node && node->next && node->next->next) {
21+
node->data = node->next->data;
22+
node = node->next;
23+
}
24+
node->data = node->next->data;
25+
node->next = nullptr;
826
return;
927
}
1028
shared_ptr<ListNode<int>> DeletionFromListWrapper(

elements-of-programming-interviews/cpp/is_list_cyclic.cc

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,94 @@
11
#include <memory>
2+
#include <set>
23

34
#include "list_node.h"
45
#include "test_framework/generic_test.h"
56
#include "test_framework/test_failure.h"
67
#include "test_framework/timed_executor.h"
8+
using std::set;
79
using std::shared_ptr;
810

11+
// Normal way: tortise and hare method; list must be finite. But this doesn't
12+
// return the first node of the cycle.
13+
14+
// O(n) space - just use a set.
15+
16+
// O(c) space, O(n^2) time; use Floyd's method (tortise/hare) to find cycle.
17+
// Then, start another pointer (turtle; moves one at a time) off from the start
18+
// of the list. Each time turtle moves, have the tortise pointer do a full lap
19+
// until it reaches either the hare or the turtle. If tortise reaches hare,
20+
// turtle is not on cycle yet. If tortise reaches turtle, turtle has reached
21+
// first node of cycle.
22+
23+
shared_ptr<ListNode<int>> HasCycleLinearSpace(
24+
const shared_ptr<ListNode<int>>& head) {
25+
shared_ptr<ListNode<int>> curr = head;
26+
set<shared_ptr<ListNode<int>>> visited;
27+
while (curr && visited.find(curr) == visited.end()) {
28+
visited.insert(curr);
29+
curr = curr->next;
30+
}
31+
return curr;
32+
}
33+
34+
/*
35+
- Suppose we use the Tortise/Hare strategy to force the two pointers ("slow"
36+
and "fast") to meet.
37+
- Assume this list:
38+
_______________
39+
V |
40+
0 -> 1 -> 2 -> 3 -> 4 -> 5
41+
42+
- The linked list can be described using the following:
43+
- A: Nodes not in cycles (2: 0, and 1)
44+
- B: Distance from start of cycle meeting point (2)
45+
- (0,0), (1,2), (2,4), (3, 2), (4,4)
46+
- C: cycle length (4: 2,3,4,5)
47+
- When slow meets fast, slow traveled through all of the non-cycle nodes (A)
48+
and the distance from the first cycle node to the meeting point to B, so
49+
A+B.
50+
- Because fast moves 2 nodes for each of slow's 1, fast has moved 2(A+B)
51+
when they meet; the extra A+B that fast has moved before meeting slow happens
52+
inside the loop.
53+
- Suppose we now reset slow; fast is still at the meeting point:
54+
- Slow is now A away from the first node in the cycle
55+
- Fast is C-B away from the first node in the cycle.
56+
- If A = C-B, then they meet at the first node of the cycle.
57+
- When hare traveled the first A+B, it arrived at the meeting point.
58+
When it then traveled another A+B, it arrived at the meeting point
59+
again. This does _not_ mean that C = A+B, but it does mean that C*N =
60+
A+B.
61+
- If N = 1 (so C = A+B), then when both nodes travel A forward, they will
62+
meet at the first node of the cycle.
63+
- If N > 1, then C < A+B, so hare completes several cycles as tortise
64+
moves A. However, after some number of cycles, tortise will be C away from the
65+
meeting point, and hare will be at the meeting point. Hare moves forward C-B
66+
to get to the first cycle point. Turtle will also move forward C-B along the
67+
non-cycle points, arriving simultaneously at the first cycle point.
68+
69+
70+
*/
71+
972
shared_ptr<ListNode<int>> HasCycle(const shared_ptr<ListNode<int>>& head) {
10-
// TODO - you fill in here.
11-
return nullptr;
73+
shared_ptr<ListNode<int>> slow = head, fast = head;
74+
do {
75+
if (!slow || !fast || !fast->next) {
76+
// no cycle
77+
return nullptr;
78+
}
79+
80+
slow = slow->next;
81+
fast = fast->next->next;
82+
} while (fast != slow);
83+
84+
slow = head;
85+
while (fast != slow) {
86+
fast = fast->next;
87+
slow = slow->next;
88+
}
89+
return fast;
1290
}
91+
1392
void HasCycleWrapper(TimedExecutor& executor,
1493
const shared_ptr<ListNode<int>>& head, int cycle_idx) {
1594
int cycle_length = 0;

elements-of-programming-interviews/cpp/reverse_sublist.cc

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
#include "list_node.h"
22
#include "test_framework/generic_test.h"
33

4+
/*
5+
0 1 2 3 4 5 6
6+
A -> B -> C -> D -> E -> F -> G
7+
8+
ReverseSublist(2, 5)
9+
10+
0 1 2 3 4 5 6
11+
A -> B -> F -> E -> D -> C -> G
12+
13+
14+
15+
- Have two pointers, to 5 (end, final) and 2 (start).
16+
while (start != final)
17+
- Set 2->next to 5->next
18+
- (start->next = end->next; end = start, start = start->next)
19+
- Then set 3->next to 2
20+
- Then set 4->next to 3
21+
- Then set 5->next to 4
22+
23+
24+
*/
25+
426
shared_ptr<ListNode<int>> ReverseSublist(shared_ptr<ListNode<int>> L, int start,
527
int finish) {
628
// TODO - you fill in here.
7-
return nullptr;
29+
return L;
830
}
931

1032
int main(int argc, char* argv[]) {

elements-of-programming-interviews/cpp/sorted_lists_merge.cc

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
#include "list_node.h"
22
#include "test_framework/generic_test.h"
3+
4+
// One / both could be empty
5+
// Negatives?
6+
37
shared_ptr<ListNode<int>> MergeTwoSortedLists(shared_ptr<ListNode<int>> L1,
48
shared_ptr<ListNode<int>> L2) {
5-
// TODO - you fill in here.
6-
return nullptr;
9+
shared_ptr<ListNode<int>> head = make_shared<ListNode<int>>();
10+
shared_ptr<ListNode<int>> curr = head;
11+
while (L1 && L2) {
12+
if (L1->data <= L2->data) {
13+
curr->next = L1;
14+
L1 = L1->next;
15+
} else {
16+
curr->next = L2;
17+
L2 = L2->next;
18+
}
19+
curr = curr->next;
20+
}
21+
if (L1) {
22+
curr->next = L1;
23+
}
24+
if (L2) {
25+
curr->next = L2;
26+
}
27+
28+
return head->next;
729
}
830

931
int main(int argc, char* argv[]) {

elements-of-programming-interviews/problem_mapping.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ problem_mapping = {
690690
},
691691
"7.01 Merge two sorted lists": {
692692
"C++: sorted_lists_merge.cc": {
693-
"passed": 0,
693+
"passed": 501,
694694
"total": 501
695695
},
696696
"Java: SortedListsMerge.java": {
@@ -704,7 +704,7 @@ problem_mapping = {
704704
},
705705
"7.02 Reverse a single sublist": {
706706
"C++: reverse_sublist.cc": {
707-
"passed": 0,
707+
"passed": 2,
708708
"total": 210
709709
},
710710
"Java: ReverseSublist.java": {
@@ -718,7 +718,7 @@ problem_mapping = {
718718
},
719719
"7.03 Test for cyclicity": {
720720
"C++: is_list_cyclic.cc": {
721-
"passed": 0,
721+
"passed": 102,
722722
"total": 102
723723
},
724724
"Java: IsListCyclic.java": {
@@ -760,7 +760,7 @@ problem_mapping = {
760760
},
761761
"7.06 Delete a node from a singly linked list": {
762762
"C++: delete_node_from_list.cc": {
763-
"passed": 0,
763+
"passed": 512,
764764
"total": 512
765765
},
766766
"Java: DeleteNodeFromList.java": {

0 commit comments

Comments
 (0)