linked-list-cycle 1.0.0
Linked List Cycle
Loading...
Searching...
No Matches
main.cpp File Reference
#include <bits/stdc++.h>
Include dependency graph for main.cpp:

Go to the source code of this file.

Data Structures

struct  ListNode
 Definition for singly-linked list. More...
 
class  Solution
 

Functions

ListNodecreate_list (const vector< int > &array, int pos)
 
void print_list (ListNode *head)
 
void delete_list (ListNode *head, set< ListNode * > *handled=NULL)
 
int main ()
 

Function Documentation

◆ create_list()

ListNode * create_list ( const vector< int > &  array,
int  pos 
)

Definition at line 15 of file main.cpp.

16{
17 if (array.size() == 0)
18 return NULL;
19 ListNode* head = new ListNode(array[0]);
20 ListNode* current = head;
21 ListNode* cycling_node = NULL;
22 for (int i = 1; i < (int)array.size(); ++i)
23 {
24 current->next = new ListNode(array[i]);
25 current = current->next;
26 if (i == pos)
27 cycling_node = current;
28 }
29 if (pos == 0)
30 current->next = head;
31 else
32 current->next = cycling_node;
33 return head;
34}
Definition for singly-linked list.
Definition main.cpp:9
ListNode * next
Definition main.cpp:11

References ListNode::next.

Referenced by main().

◆ delete_list()

void delete_list ( ListNode head,
set< ListNode * > *  handled = NULL 
)

Definition at line 52 of file main.cpp.

53{
54 bool created = false;
55 if (!handled)
56 {
57 handled = new set<ListNode*>;
58 created = true;
59 }
60 handled->insert(head);
61 if (handled->find(head->next) == handled->end() && head->next)
62 delete_list(head->next, handled);
63 delete head;
64 if (created)
65 delete handled;
66}
void delete_list(ListNode *head, set< ListNode * > *handled=NULL)
Definition main.cpp:52

References delete_list(), and ListNode::next.

Referenced by delete_list(), and main().

◆ main()

int main ( )

Definition at line 112 of file main.cpp.

113{
114 vector<int> input = {3,2,0,-4};
115 int pos = 1;
116 ListNode* head = create_list(input, pos);
117 print_list(head);
118
119 cout << "Does the list contain a cycle? " << Solution().hasCycle(head) << endl;
120
121 delete_list(head);
122 return 0;
123}
bool hasCycle(ListNode *head)
Check if the list has a cycle using fast and slow pointers.
Definition main.cpp:91
ListNode * create_list(const vector< int > &array, int pos)
Definition main.cpp:15
void print_list(ListNode *head)
Definition main.cpp:36

References create_list(), delete_list(), Solution::hasCycle(), and print_list().

◆ print_list()

void print_list ( ListNode head)

Definition at line 36 of file main.cpp.

37{
38 ListNode* current = head;
39 set<ListNode*> visited;
40 while (current != NULL)
41 {
42 if (visited.find(current) != visited.end())
43 break;
44 visited.insert(current);
45 cout << current->val << endl;
46 current = current->next;
47 }
48 if (current) // if cycle exists
49 cout << "next in cycle: " << current->val << endl;
50}
int val
Definition main.cpp:10

References ListNode::next, and ListNode::val.

Referenced by main().