commit 36854e82f1b5605249853a1213464cab62ee70a6
parent ee2999fecc3f915e0758a7968618e0a644077092
Author: Jack Sangdahl <jack@pngu.org>
Date: Mon, 15 Jun 2026 10:24:17 -0600
2095. delete the middle node of a linked list
Diffstat:
2 files changed, 82 insertions(+), 0 deletions(-)
diff --git a/delete_middle_node_linked_list.c b/delete_middle_node_linked_list.c
@@ -0,0 +1,44 @@
+// 2095. Delete the Middle Node of a Linked List
+#include "leetcode.h"
+
+/*
+ * you are given the 'head' of a linked list. delete the middle node, and return
+ * the 'head' of the modified linked list. the middle of a linked list of size
+ * 'n' is the 'floor(n / 2)'th node from the start using 0-based indexing, where
+ * 'floor(x)' denotes the largest integer less than or equal to 'x'.
+ */
+
+struct ListNode *deleteMiddle(struct ListNode *head) {
+ struct ListNode *prev = (struct ListNode *)malloc(sizeof(struct ListNode));
+ prev->val = 0;
+ prev->next = head;
+ struct ListNode *slow = prev, *fast = head;
+ while (fast && fast->next) {
+ slow = slow->next;
+ fast = fast->next->next;
+ }
+ struct ListNode *tmp = slow->next;
+ slow->next = slow->next->next;
+ free(tmp);
+ struct ListNode *ans = prev->next;
+ free(prev);
+ return ans;
+}
+
+int main() {
+ int h1i[] = {1, 3, 4, 7, 1, 2, 6}, r1[] = {1, 3, 4, 1, 2, 6};
+ int h2i[] = {1, 2, 3, 4}, r2[] = {1, 2, 4};
+ int h3i[] = {2, 1}, r3[] = {2};
+ struct ListNode *h1 = listnode_build(h1i, ARRAY_SIZE(h1i));
+ struct ListNode *h2 = listnode_build(h2i, ARRAY_SIZE(h2i));
+ struct ListNode *h3 = listnode_build(h3i, ARRAY_SIZE(h3i));
+ struct ListNode *dm1 = deleteMiddle(h1);
+ struct ListNode *dm2 = deleteMiddle(h2);
+ struct ListNode *dm3 = deleteMiddle(h3);
+ listnode_print(dm1, r1);
+ listnode_print(dm2, r2);
+ listnode_print(dm3, r3);
+ listnode_free(h1);
+ listnode_free(h2);
+ listnode_free(h3);
+}
diff --git a/delete_middle_node_linked_list.py b/delete_middle_node_linked_list.py
@@ -0,0 +1,38 @@
+# 2095. Delete the Middle Node of a Linked List
+from typing import Optional
+
+from leetcode import ListNode, listnode_build, listnode_print
+
+"""
+you are given the 'head' of a linked list. delete the middle node, and return
+the 'head' of the modified linked list. the middle of a linked list of size
+'n' is the 'floor(n / 2)'th node from the start using 0-based indexing, where
+'floor(x)' denotes the largest integer less than or equal to 'x'.
+"""
+
+
+class Solution:
+ def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
+ prev = ListNode(0)
+ prev.next = head
+ slow = prev
+ fast = head
+ while fast != None and fast.next != None:
+ slow = slow.next
+ fast = fast.next.next
+
+ slow.next = slow.next.next
+ return prev.next
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ h1 = listnode_build(vals=[1, 3, 4, 7, 1, 2, 6])
+ h2 = listnode_build(vals=[1, 2, 3, 4])
+ h3 = listnode_build(vals=[2, 1])
+ r1 = obj.deleteMiddle(h1)
+ r2 = obj.deleteMiddle(h2)
+ r3 = obj.deleteMiddle(h3)
+ listnode_print(r1)
+ listnode_print(r2)
+ listnode_print(r3)