Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package g2001_2100.s2095_delete_the_middle_node_of_a_linked_list;

// #Medium #Linked List #Two Pointers
// 2022_05_23_Time_4_ms_(95.33%)_Space_220.1_MB_(38.17%)

import com_github_leetcode.ListNode;

/*
* 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; }
* }
*/

public class Solution {
public ListNode deleteMiddle(ListNode head) {
if (head.next == null) return null;
ListNode slow = head;
ListNode fast = head.next.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
slow.next = slow.next.next;
return head;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
2095. Delete the Middle Node of a Linked List

Medium

You are given the `head` of a linked list. **Delete** the **middle node** , and return *the* `head` *of the modified linked list* .

The **middle node** of a linked list of size `n` is the `⌊n / 2⌋<sup>th</sup>` node from the **start** using **0-based indexing** , where `⌊x⌋` denotes the largest integer less than or equal to `x`.

* For `n` = `1`, `2`, `3`, `4`, and `5`, the middle nodes are `0`, `1`, `1`, `2`, and `2`, respectively.

**Example 1:**

![](image/readme/1653301634882.png)

**Input:** head = [1,3,4,7,1,2,6]

**Output:** [1,3,4,1,2,6]

**Explanation:**

The above figure represents the given linked list. The indices of the nodes are written below. Since n = 7, node 3 with value 7 is the middle node, which is marked in red. We return the new list after removing this node.

**Example 2:**

![img](image/readme/1653301750855.png)

**Input:** head = [1,2,3,4]

**Output:** [1,2,4]

**Explanation:**

The above figure represents the given linked list. For n = 4, node 2 with value 3 is the middle node, which is marked in red.

**Example 3:**

![](image/readme/1653301837206.png)

**Input:** head = [1,2]

**Output:** [2]

**Explanation:**

The above figure represents the given linked list. For n = 2, node 1 with value 1 is the middle node, which is marked in red. Node 0 with value 2 is the only node remaining after removing node 1.

**Constraints:**

* The number of nodes in the list is in the range `[1, 10<sup>5</sup>]`.
* `1 <= Node.val <= 10<sup>5</sup>`
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g2001_2100.s2095_delete_the_middle_node_of_a_linked_list;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import com_github_leetcode.LinkedListUtils;
import com_github_leetcode.ListNode;
import java.util.Arrays;
import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void deleteMiddle() {
ListNode node1 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 3, 4, 7, 1, 2, 6));
assertThat(new Solution().deleteMiddle(node1).toString(), equalTo("1, 3, 4, 1, 2, 6"));
}

@Test
void deleteMiddle2() {
ListNode node2 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 2, 3, 4));
assertThat(new Solution().deleteMiddle(node2).toString(), equalTo("1, 2, 4"));
}

@Test
void deleteMiddle3() {
ListNode node3 = LinkedListUtils.createSinglyLinkedList(Arrays.asList(2, 1));
assertThat(new Solution().deleteMiddle(node3).toString(), equalTo("2"));
}
}