{"id":"078880a7-0436-4315-a35a-bde749095f12","name":"Is Cycle Present In Linkedlist","description":"1. Given a singly linklist. determine if the linked list has a cycle in it.\r\n2. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.\r\n","inputFormat":"input is handle for you\r\n","outputFormat":"output is handle for you.\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nbool isCyclePresentInLL(ListNode *head)\r\n{\r\n return false;\r\n}\r\n\r\nListNode *takeInput()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n int idx;\r\n cin >> idx;\r\n if (idx >= 0)\r\n {\r\n ListNode *curr = dummy->next;\r\n while (idx-- > 0)\r\n {\r\n curr = curr->next;\r\n }\r\n prev->next = curr;\r\n }\r\n return dummy->next;\r\n}\r\n\r\nint main()\r\n{\r\n ListNode *head = takeInput();\r\n cout << (boolalpha) << isCyclePresentInLL(head) << endl;\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static boolean isCyclePresentInLL(ListNode head) {\r\n\r\n return false;\r\n }\r\n\r\n public static ListNode takeInput() {\r\n int n = scn.nextInt();\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n int idx = scn.nextInt();\r\n if (idx >= 0) {\r\n ListNode curr = dummy.next;\r\n while (idx-- > 0) {\r\n curr = curr.next;\r\n }\r\n prev.next = curr;\r\n }\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n ListNode head = takeInput();\r\n System.out.println(isCyclePresentInLL(head));\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"8\r\n1\r\n18\r\n1\r\n8\r\n-1\r\n138\r\n31\r\n84\r\n3","sampleOutput":"true\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"56e29d74-97f7-498e-a4bf-7a0a849af293","name":"Is Cycle Present In Linkedlist","slug":"is-cycle-present-in-linkedlist","type":1}],"next":{"id":"ba0c5df2-9357-422a-8f6c-962082f8a33d","name":"Is Cycle Present In Linkedlist MCQ","type":0,"slug":"is-cycle-present-in-linkedlist-mcq"},"prev":{"id":"dbcf353d-8468-466a-bef6-f9158f38edd8","name":"Copy LinkedList With Random Pointers","type":3,"slug":"copy-linkedlist-with-random-pointers"}}}

Is Cycle Present In Linkedlist

1. Given a singly linklist. determine if the linked list has a cycle in it. 2. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.

{"id":"078880a7-0436-4315-a35a-bde749095f12","name":"Is Cycle Present In Linkedlist","description":"1. Given a singly linklist. determine if the linked list has a cycle in it.\r\n2. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.\r\n","inputFormat":"input is handle for you\r\n","outputFormat":"output is handle for you.\r\n","constraints":"0 &lt;= N &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nbool isCyclePresentInLL(ListNode *head)\r\n{\r\n return false;\r\n}\r\n\r\nListNode *takeInput()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n int idx;\r\n cin >> idx;\r\n if (idx >= 0)\r\n {\r\n ListNode *curr = dummy->next;\r\n while (idx-- > 0)\r\n {\r\n curr = curr->next;\r\n }\r\n prev->next = curr;\r\n }\r\n return dummy->next;\r\n}\r\n\r\nint main()\r\n{\r\n ListNode *head = takeInput();\r\n cout << (boolalpha) << isCyclePresentInLL(head) << endl;\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static boolean isCyclePresentInLL(ListNode head) {\r\n\r\n return false;\r\n }\r\n\r\n public static ListNode takeInput() {\r\n int n = scn.nextInt();\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n int idx = scn.nextInt();\r\n if (idx >= 0) {\r\n ListNode curr = dummy.next;\r\n while (idx-- > 0) {\r\n curr = curr.next;\r\n }\r\n prev.next = curr;\r\n }\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n ListNode head = takeInput();\r\n System.out.println(isCyclePresentInLL(head));\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"8\r\n1\r\n18\r\n1\r\n8\r\n-1\r\n138\r\n31\r\n84\r\n3","sampleOutput":"true\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"56e29d74-97f7-498e-a4bf-7a0a849af293","name":"Is Cycle Present In Linkedlist","slug":"is-cycle-present-in-linkedlist","type":1}],"next":{"id":"ba0c5df2-9357-422a-8f6c-962082f8a33d","name":"Is Cycle Present In Linkedlist MCQ","type":0,"slug":"is-cycle-present-in-linkedlist-mcq"},"prev":{"id":"dbcf353d-8468-466a-bef6-f9158f38edd8","name":"Copy LinkedList With Random Pointers","type":3,"slug":"copy-linkedlist-with-random-pointers"}}}
plane

Editor


Loading...

Is Cycle Present In Linkedlist

easy

1. Given a singly linklist. determine if the linked list has a cycle in it. 2. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.

Constraints

0 <= N <= 10^6

Format

Input

input is handle for you

Output

output is handle for you.

Example

Sample Input

8 1 18 1 8 -1 138 31 84 3

Sample Output

true

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode