Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2055 from andrewmustea/add_876-middle-…
Browse files Browse the repository at this point in the history
…of-the-linked-list.c

create 0876-middle-of-the-linked-list.c
  • Loading branch information
tahsintunan authored Jan 18, 2023
2 parents c54f7ac + d71120a commit 0890e9f
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions c/0876-middle-of-the-linked-list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Given the head of a singly linked list, return the middle node of the linked
* list.
*
* If there are two middle nodes, return the second middle node.
*
* Constraints:
*
* The number of nodes in the list is in the range [1, 100].
* 1 <= Node.val <= 100
*
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*
* Space = O(1)
* Time = O(n)
*/

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

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

return slow;
}

0 comments on commit 0890e9f

Please sign in to comment.