[LeetCode] 2095. Delete the Middle Node of a Linked List as the intermediate node of the chain list

Keywords: Python C++ Algorithm leetcode linked list

  • Author: Ming Xue Candle
  • id: fuxuemingzhu
  • Personal blog: http://fuxuemingzhu.cn/
  • Public number: snowy candles
  • Key words in this article: Leetcode, buckle, brush title, Python, C++, chain list, intermediate node, delete

Title Address: https://leetcode-cn.com/problems/delete-the-middle-node-of-a-linked-list/

Title Description

Give you the head node of the list. Deletes the intermediate node of the chain table and returns the head of the modified chain table.

The middle node of the N-linked list is the n/2 node from the beginning (subscript starts at 0), where x represents the largest integer less than or equal to x.

For n = 1, 2, 3, 4, and 5, the subscripts of intermediate 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 diagram above shows the given list of chains. The subscripts of the nodes are labeled below each node.
Because n = 7 ,Node 3 with a value of 7 is the intermediate node, labeled in red.
Returns a new list of chains after removing nodes. 

Example 2:

Input: head = [1,2,3,4]
Output:[1,2,4]
Explanation:
The diagram above shows the given list of chains.
about n = 4 ,Node 2 with a value of 3 is the intermediate node, labeled in red.

Example 3:

Input: head = [2,1]
Output:[2]
Explanation:
The diagram above shows the given list of chains.
about n = 2 ,Node 1 with a value of 1 is the intermediate node, labeled in red.
Node 0 with a value of 2 is the only node left after node 1 is removed.

Tips:

  1. The number of nodes in the list is in the range [1, 10^5]
  2. 1 <= Node.val <= 105

Topic Essentials

Delete the intermediate node of the chain table, pay attention to the definition of the intermediate node: when the length of the chain table is odd, the intermediate node is more obvious; When the chain list length is even, the middle node is actually the right node inside the middle two nodes.

Problem Solving Strategies

There are two ways to do this.

Length before walking

That's what I did during the week. It's easier.

The length of the list is calculated first, then the subscript of the intermediate node is calculated, and then it is deleted.

Note: If we want to delete a node in the list, we must find the previous node of this node.

Chain list length / 2 - 1 is the position of the previous node of the intermediate node.

  • For example, if the chain length is 3, then the chain length / 2 - 1 = 0 is the position of the previous node of the intermediate node.
  • For example, if the chain length is 4, then the chain length / 2 - 1 = 1 is the position of the previous node of the intermediate node.

The simple way to delete a list node is to move.next = move.next.next.

The Python code is as follows:

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def deleteMiddle(self, head):
        """
        :type head: Optional[ListNode]
        :rtype: Optional[ListNode]
        """
        size = 0
        move = head
        while move:
            size += 1
            move = move.next
        if size == 1:
            return None
        move = head
        for i in range(size // 2 - 1):
            move = move.next
        move.next = move.next.next
        return head

Fast slow pointer

If you've done a lot with the list titles, it's easy to think of slow and fast pointers.

The fast pointer takes two steps at a time; Slow pointer takes one step at a time; Then when the fast pointer reaches the end of the list, the slow pointer is just in the middle.

Since we need to find the previous node of the intermediate node, we need a prev node to hold the position before the slow pointer.

Finally, use prev->next = prev->next->next; Delete the intermediate node.

It is important to note that when there is only one node in the input of the chain table, the null pointer is returned directly and can be judged from the very beginning of the function. Do not run the fast and slow pointer at a node, as the fast and slow pointers do not remove the header node.

The C++ code is as follows:

/**
 * 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) {}
 * };
 */
class Solution {
public:
    ListNode* deleteMiddle(ListNode* head) {
        if (!head->next)
            return nullptr;
        ListNode* fast = head;
        ListNode* slow = head;
        ListNode* prev = slow;
        while (fast && fast->next) {
            fast = fast->next->next;
            prev = slow;
            slow = slow->next;
        }
        prev->next = prev->next->next;
        return head;
    }
};

date

December 6, 2021 - It's nice to see a house bought by someone else

Posted by Shendemiar on Mon, 06 Dec 2021 09:36:46 -0800