Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2057 from andrewmustea/add_0142-linked…
Browse files Browse the repository at this point in the history
…-list-cycle-ii.c

create 0142-linked-list-cycle-ii.c
  • Loading branch information
tahsintunan authored Jan 24, 2023
2 parents d88e61e + 69eb3f7 commit 284bf69
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions c/0142-linked-list-cycle-ii.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Given the head of a linked list, return the node where the cycle begins. If
* there is no cycle, return null.
*
* 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. Internally, pos
* is used to denote the index of the node that tail's next pointer is connected
* to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as
* a parameter.
*
* Do not modify the linked list.
*
* Constraints:
*
* The number of the nodes in the list is in the range [0, 104].
* -105 <= Node.val <= 105
* pos is -1 or a valid index in the linked-list.
*
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* Space: O(1)
* Time: O(n)
*/

struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode *fast = head;
struct ListNode *slow = head;

if (!head)
return NULL;

while (fast->next && fast->next->next) {
fast = fast->next->next;
slow = slow->next;

if (fast == slow) {
/**
* The index of the node cycle is located the same number of nodes
* away from the start of the linked list and the intersection of
* the slow and fast pointers.
*/
struct ListNode *head_node = head;
struct ListNode *intersection = slow;

while (head_node != intersection) {
head_node = head_node->next;
intersection = intersection->next;
}

return intersection;
}
}

return NULL;
}

0 comments on commit 284bf69

Please sign in to comment.