`{"id":"7dcd4032-f788-4bc2-a70d-ca74ea548285","name":"Cycle Node In Linkedlist","description":"\r\n1. Given a linked list, return the node where the cycle begins. If there is no cycle, return null.\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\r\nNotice that you should not modify the linked list.","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\nListNode *CycleNode(ListNode *head)\r\n{\r\n return nullptr;\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 ListNode *ans = CycleNode(head);\r\n cout << (ans != nullptr ? ans->val : -1) << 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 ListNode CycleNode(ListNode head) {\r\n return null;\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 ListNode ans = CycleNode(head);\r\n System.out.println(ans!=null?ans.val:-1);\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":"8\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":"b1a2d1f8-d242-45bd-9021-a90992c37451","name":"Cycle Node In Linkedlist","slug":"cycle-node-in-linkedlist","type":1}],"next":{"id":"b4346f9c-7611-44a9-b31c-f80ea2dfc664","name":"Cycle Node In Linked List MCQ","type":0,"slug":"cycle-node-in-linked-list-mcq"},"prev":{"id":"b7c20dfd-806d-438d-b4b1-2176aca68e2f","name":"Right View Of A Binary Tree","type":1,"slug":"right-view-of-a-binary-tree"}}}`

# Cycle Node In Linkedlist

1. Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 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. Notice that you should not modify the linked list.

`{"id":"7dcd4032-f788-4bc2-a70d-ca74ea548285","name":"Cycle Node In Linkedlist","description":"\r\n1. Given a linked list, return the node where the cycle begins. If there is no cycle, return null.\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\r\nNotice that you should not modify the linked list.","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\nListNode *CycleNode(ListNode *head)\r\n{\r\n return nullptr;\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 ListNode *ans = CycleNode(head);\r\n cout << (ans != nullptr ? ans->val : -1) << 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 ListNode CycleNode(ListNode head) {\r\n return null;\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 ListNode ans = CycleNode(head);\r\n System.out.println(ans!=null?ans.val:-1);\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":"8\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":"b1a2d1f8-d242-45bd-9021-a90992c37451","name":"Cycle Node In Linkedlist","slug":"cycle-node-in-linkedlist","type":1}],"next":{"id":"b4346f9c-7611-44a9-b31c-f80ea2dfc664","name":"Cycle Node In Linked List MCQ","type":0,"slug":"cycle-node-in-linked-list-mcq"},"prev":{"id":"b7c20dfd-806d-438d-b4b1-2176aca68e2f","name":"Right View Of A Binary Tree","type":1,"slug":"right-view-of-a-binary-tree"}}}` Editor

# Cycle Node In Linkedlist

easy

1. Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 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. Notice that you should not modify the linked list.

0 <= N <= 10^6

## Format

### Input

input is handle for you

### Output

output is handle for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}8 1 18 1 8 -1 138 31 84 3```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}8 ```

Discussions

Show Discussion

Related Resources 