Skip to content

Commit 502ad0f

Browse files
committed
Delete the Middle Node of a Linked List
1 parent 9f10a2c commit 502ad0f

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// 2095. Delete the middle node of a linked list
2+
// Topics: 'Linked List', 'Two Pointers'
3+
4+
// You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.
5+
6+
// The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.
7+
8+
// For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.
9+
10+
// Example 1:
11+
12+
// Input: head = [1,3,4,7,1,2,6]
13+
// Output: [1,3,4,1,2,6]
14+
// Explanation:
15+
// The above figure represents the given linked list. The indices of the nodes are written below.
16+
// Since n = 7, node 3 with value 7 is the middle node, which is marked in red.
17+
// We return the new list after removing this node.
18+
19+
// Example 2:
20+
21+
// Input: head = [1,2,3,4]
22+
// Output: [1,2,4]
23+
// Explanation:
24+
// The above figure represents the given linked list.
25+
// For n = 4, node 2 with value 3 is the middle node, which is marked in red.
26+
27+
// Example 3:
28+
29+
// Input: head = [2,1]
30+
// Output: [2]
31+
// Explanation:
32+
// The above figure represents the given linked list.
33+
// For n = 2, node 1 with value 1 is the middle node, which is marked in red.
34+
// Node 0 with value 2 is the only node remaining after removing node 1.
35+
36+
// Constraints:
37+
38+
// The number of nodes in the list is in the range [1, 105].
39+
// 1 <= Node.val <= 105
40+
41+
package deletethemidnodeoflinkedlist
42+
43+
func deleteMiddle(head *ListNode) *ListNode {
44+
if head.Next == nil {
45+
return head.Next
46+
}
47+
slow := head
48+
fast := head
49+
for {
50+
fast = fast.Next.Next
51+
if fast == nil || fast.Next == nil {
52+
break
53+
}
54+
slow = slow.Next
55+
}
56+
slow.Next = slow.Next.Next
57+
return head
58+
}
59+
60+
type ListNode struct {
61+
Val int
62+
Next *ListNode
63+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package deletethemidnodeoflinkedlist
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestDeleteMiddle(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
input []int
11+
expected []int
12+
}{
13+
{
14+
name: "seven nodes",
15+
input: []int{1, 3, 4, 7, 1, 2, 6},
16+
expected: []int{1, 3, 4, 1, 2, 6},
17+
},
18+
{
19+
name: "four nodes",
20+
input: []int{1, 2, 3, 4},
21+
expected: []int{1, 2, 4},
22+
},
23+
{
24+
name: "two nodes",
25+
input: []int{2, 1},
26+
expected: []int{2},
27+
},
28+
{
29+
name: "single node",
30+
input: []int{1},
31+
expected: []int{},
32+
},
33+
{
34+
name: "three nodes",
35+
input: []int{1, 2, 3},
36+
expected: []int{1, 3},
37+
},
38+
{
39+
name: "five nodes",
40+
input: []int{1, 2, 3, 4, 5},
41+
expected: []int{1, 2, 4, 5},
42+
},
43+
{
44+
name: "six nodes",
45+
input: []int{1, 2, 3, 4, 5, 6},
46+
expected: []int{1, 2, 3, 5, 6},
47+
},
48+
}
49+
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
head := buildList(tt.input)
53+
result := deleteMiddle(head)
54+
actual := listToSlice(result)
55+
56+
if !equal(actual, tt.expected) {
57+
t.Errorf("got %v, want %v", actual, tt.expected)
58+
}
59+
})
60+
}
61+
}
62+
63+
func buildList(vals []int) *ListNode {
64+
if len(vals) == 0 {
65+
return nil
66+
}
67+
head := &ListNode{Val: vals[0]}
68+
current := head
69+
for i := 1; i < len(vals); i++ {
70+
current.Next = &ListNode{Val: vals[i]}
71+
current = current.Next
72+
}
73+
return head
74+
}
75+
76+
func listToSlice(head *ListNode) []int {
77+
result := []int{}
78+
for head != nil {
79+
result = append(result, head.Val)
80+
head = head.Next
81+
}
82+
return result
83+
}
84+
85+
func equal(a, b []int) bool {
86+
if len(a) != len(b) {
87+
return false
88+
}
89+
for i := range a {
90+
if a[i] != b[i] {
91+
return false
92+
}
93+
}
94+
return true
95+
}

0 commit comments

Comments
 (0)