-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-the-middle-node-of-a-linked-list_2095.py
89 lines (67 loc) · 2.48 KB
/
delete-the-middle-node-of-a-linked-list_2095.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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⌋th 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:
# 
# 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:
# 
# 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:
# 
# Input: head = [2,1]
# 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.
# ---------------------------------------Runtime 1267 ms Beats 83% Memory 62.9 MB Beats 49.35%---------------------------------------
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# Solution 1
# Time complexity O(n)
class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
length = 0
node = head
while node:
length += 1
node = node.next
target = length // 2
if length <= 2:
head.next = None
return head if length != 1 else None
node = head
next_node = None
count = 0
while count != target and node:
node = node.next
count += 1
next_node = node.next
node.val = None if not next_node else next_node.val
node.next = None if not next_node else next_node.next
return head
# Solution 2
# Time complexity O(n)
class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.next == None:
return None
slow, fast = head, head.next.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
slow.next = slow.next.next
return head