Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
median_search2.cpp File Reference

Given a linked list L[0,....,n] of n numbers, find the middle node. More...

#include <cassert>
#include <iostream>
Include dependency graph for median_search2.cpp:

Classes

struct  ListNode
 for IO operations More...
 

Namespaces

namespace  search
 for std::vector
 
 

Functions

ListNodesearch::median_search2::middleNode (ListNode *head)
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Given a linked list L[0,....,n] of n numbers, find the middle node.

The technique utilized in this implementation is the ["Floyd's tortoise and hare"](https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare) approach. This technique uses two pointers that iterate through the list at different 'speeds' in order to solve problems. In this implementation, for every iteration the slow pointer advances one node while the fast pointer advances two nodes. The result of this is that since the fast pointer moves twice as fast as the slow pointer, when the fast pointer reaches the end of the list the slow pointer will be pointing to the middle node of the list.

Here are some example lists you can use to see how the algorithm works A = [1,2,3,4,5] B = [1,2,3,4,5,6] print median(A) #should be 39 print median(B) #should be 4

Author
Benjamin Weiss
See also
median_search.cpp

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
136 {
137 test(); // run self-test implementations
138 return 0;
139}
static void test()
Self-test implementations.
Definition median_search2.cpp:83
Here is the call graph for this function:

◆ middleNode()

ListNode * search::median_search2::middleNode ( ListNode * head)

This function searches for the median of a linked list.

Parameters
headThe head of the linked list.
Returns
Median node of the linked list.
59 {
60 if (!head) {
61 return nullptr;
62 }
63
64 // Fast and slow pointers
65 ListNode* fastptr = nullptr;
66 ListNode* slowptr = fastptr = head;
67
68 // fast jumps 2 while slow jumps 1
69 while (fastptr->next && fastptr->next->next) {
70 slowptr = slowptr->next;
71 fastptr = fastptr->next->next;
72 }
73
74 return (fastptr->next) ? slowptr->next : slowptr;
75}
for IO operations
Definition median_search2.cpp:31
ListNode * next
pointer to the next node
Definition median_search2.cpp:33
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
83 {
84 auto* head1 = new ListNode;
85 head1->val = 1;
86
87 ListNode* temp = head1;
88 for (int i = 2; i < 6; ++i) {
89 // Allocate next
90 auto* temp1 = new ListNode;
91 temp1->val = i;
92
93 // Advance
94 temp->next = temp1;
95 temp = temp1;
96 }
97 temp->next = nullptr;
98
100 assert(3 == median->val); // 3 is the value of the median node.
101 std::cout << "test case:1 passed\n";
102
103 // Test case # 2
104 auto* head2 = new ListNode;
105 head2->val = 1;
106
107 ListNode* temp2 = head2;
108 for (int i = 2; i < 7; ++i) {
109 // Allocate next
110 auto* temp3 = new ListNode;
111 temp3->val = i;
112
113 // Advance
114 temp2->next = temp3;
115 temp2 = temp3;
116 }
117 temp2->next = nullptr;
118
120 assert(4 == median1->val); // 4 is the value of the median node.
121 std::cout << "test case:2 passed\n";
122
123 delete head1;
124 delete temp;
125
126 delete head2;
127 delete temp2;
128
129 std::cout << "--All tests passed--\n";
130}
ListNode * middleNode(ListNode *head)
Definition median_search2.cpp:59
int val
the value stored in the node
Definition median_search2.cpp:32