-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_cycle.h
57 lines (50 loc) · 2.39 KB
/
list_cycle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef CPP_ALGORITHM_LIST_CYCLE_H
#define CPP_ALGORITHM_LIST_CYCLE_H
#include "linked_list.h"
namespace ListCycle
{
/**
* \brief Determine if a linked list has a cycle.
* \details Given a linked list, determine if it has a cycle in it.
* \param list the head of the linked list
* \return if the linked list has a cycle, return start node of the cycle, otherwise return nullptr
*/
auto HasCycle1(const std::shared_ptr<LinkedList::Node<int>>& list)
-> std::shared_ptr<LinkedList::Node<int>>;
/**
* \brief Determine if a linked list has a cycle.
* \details Given a linked list, determine if it has a cycle in it.
* This task has O(f) + O(c) = O(n) time complexity(f is count of nodes before cycle,
* c is length of nodes in cycle, n is total nodes in list).
* \param list the head of the linked list
* \return if the linked list has a cycle, return start node of the cycle, otherwise return nullptr
*/
auto HasCycle2(const std::shared_ptr<LinkedList::Node<int>>& list)
-> std::shared_ptr<LinkedList::Node<int>>;
/**
* \brief Determine if a linked list has a cycle.
* \details Given a linked list, determine if it has a cycle in it.
* \param list the head of the linked list
* \return if the linked list has a cycle, return start index of the cycle, otherwise return -1
*/
auto HasCycle3(const std::shared_ptr<LinkedList::Node<int>>& list) -> int;
/**
* \brief Given two lists, determine if they overlap.
* \param list1 the head of the first list
* \param list2 the head of the second list
* \return if the two lists overlap, return the first overlapping node, otherwise return nullptr
*/
auto OverlappingNoCycleList(std::shared_ptr<LinkedList::Node<int>>& list1,
std::shared_ptr<LinkedList::Node<int>>& list2)
-> std::shared_ptr<LinkedList::Node<int>>;
/**
* \brief Given two lists with cycles, determine if they overlap.
* \param list1 the head of the first list
* \param list2 the head of the second list
* \return if the two lists overlap, return the first overlapping node, otherwise return nullptr
*/
auto OverlappingCycleList(std::shared_ptr<LinkedList::Node<int>>& list1,
std::shared_ptr<LinkedList::Node<int>>& list2)
-> std::shared_ptr<LinkedList::Node<int>>;
}
#endif