/**
* Problem: LeetCode 2095 - Delete the Middle Node of a Linked List
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
/**
* Problem: LeetCode 2095 - Delete the Middle Node of a Linked List
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public ListNode deleteMiddle(ListNode head) {
// If there is only one node
if (head == null || head.next == null) {
return null;
}
ListNode slow = head;
ListNode fast = head;
ListNode prev = null;
// Find the middle node
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
// Delete the middle node
prev.next = slow.next;
return head;
}
}
"""
Problem: LeetCode 2095 - Delete the Middle Node of a Linked List
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, val: int = 0, next: Optional["ListNode"] = None):
self.val = val
self.next = next
class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
# If there is only one node
if head is None or head.next is None:
return None
slow: Optional[ListNode] = head
fast: Optional[ListNode] = head
prev: Optional[ListNode] = None
# Find the middle node
while fast is not None and fast.next is not None:
prev = slow
slow = slow.next
fast = fast.next.next
# Delete the middle node
if prev is not None and slow is not None:
prev.next = slow.next
return head
/**
* Problem: LeetCode 2095 - Delete the Middle Node of a Linked List
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <iostream>
using namespace std;
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/**
* Problem: LeetCode 2095 - Delete the Middle Node of a Linked List
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
ListNode* deleteMiddle(ListNode* head) {
// If there is only one node
if (head == nullptr || head->next == nullptr) {
return nullptr;
}
ListNode* slow = head;
ListNode* fast = head;
ListNode* prev = nullptr;
// Find the middle node
while (fast != nullptr && fast->next != nullptr) {
prev = slow;
slow = slow->next;
fast = fast->next->next;
}
// Delete the middle node
prev->next = slow->next;
return head;
}
};
/**
* Problem: LeetCode 2095 - Delete the Middle Node of a Linked List
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteMiddle = function(head) {
// If there is only one node
if (head === null || head.next === null) {
return null;
}
let slow = head;
let fast = head;
let prev = null;
// Find the middle node
while (fast !== null && fast.next !== null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
// Delete the middle node
prev.next = slow.next;
return head;
};