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 <halfpacho@gmail.com>
This commit is contained in:
Sindhu Inti 2023-01-20 09:59:34 +05:30 committed by GitHub
parent 103361de54
commit e68296c07c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 1 deletions

View File

@ -120,6 +120,7 @@
| 1704 | [Determine if String Halves Are Alike](Determine if String Halves Are Alike) | [C](./src/1704.c) | Easy | | 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 | | 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 | | 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 | | 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 | | 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 | | 2222 | [Number of Ways to Select Buildings](https://leetcode.com/problems/number-of-ways-to-select-buildings/) | [C](./src/2222.c) | Medium |

View File

@ -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). 🎉 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)! 🙂 If you need any help, don't hesitate to ask and join our [**Discord server**](https://the-algorithms.com/discord)! 🙂

38
leetcode/src/2095.c Normal file
View File

@ -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;
}