From e68296c07c030c7969338807195c1b4ae7172918 Mon Sep 17 00:00:00 2001 From: Sindhu Inti <89198083+Sindhuinti@users.noreply.github.com> Date: Fri, 20 Jan 2023 09:59:34 +0530 Subject: [PATCH] feat: add delete the Middle Node of a Linked List solution (#1023) * feat:leetcode Delete the Middle Node of a Linked List solution (2095) * Update README.md * Update README.md * Update DIRECTORY.md Co-authored-by: David Leal --- leetcode/DIRECTORY.md | 1 + leetcode/README.md | 2 +- leetcode/src/2095.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 leetcode/src/2095.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index 3392c183..b0e2e764 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -120,6 +120,7 @@ | 1704 | [Determine if String Halves Are Alike](Determine if String Halves Are Alike) | [C](./src/1704.c) | Easy | | 1752 | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C](./src/1752.c) | Easy | | 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | [C](./src/2024.c) | Medium | +| 2095 | [Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/) | [C](./src/2095.c) | Medium | | 2125 | [Number of Laser Beams in a Bank](https://leetcode.com/problems/number-of-laser-beams-in-a-bank/description/) | [C](./src/2125.c) | Medium | | 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium | | 2222 | [Number of Ways to Select Buildings](https://leetcode.com/problems/number-of-ways-to-select-buildings/) | [C](./src/2222.c) | Medium | diff --git a/leetcode/README.md b/leetcode/README.md index 53931eaf..47c2b583 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -74,4 +74,4 @@ git push origin solution/your-solution-name:solution/your-solution-name 4. You're done now! You just have to make a [**pull request**](https://github.com/TheAlgorithms/C/compare). 🎉 -If you need any help, don't hesitate to ask and join our [**Discord server**](https://the-algorithms.com/discord)! 🙂 \ No newline at end of file +If you need any help, don't hesitate to ask and join our [**Discord server**](https://the-algorithms.com/discord)! 🙂 diff --git a/leetcode/src/2095.c b/leetcode/src/2095.c new file mode 100644 index 00000000..196b0892 --- /dev/null +++ b/leetcode/src/2095.c @@ -0,0 +1,38 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode* deleteMiddle(struct ListNode* head) +{ + if (head == NULL || head->next == NULL) + return NULL; + struct ListNode *fast, *slow, *prev; + int n = 0; + fast = head; + slow = head; + while (fast != NULL) + { + n = n + 1; + fast = fast->next; + } + fast = head; + while (fast->next != NULL && fast->next->next != NULL) // finds mid node + { + prev = slow; + slow = slow->next; + fast = fast->next->next; + } + if (n % 2 == 0) + { + prev = slow; + slow = slow->next; + prev->next = slow->next; + } + else + prev->next = slow->next; + return head; +}