Leetcode-- delete list node

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/EngineerHe/article/details/97683825

Leetcode- delete the list of nodes

Delete the list of nodes, including leetcode19 and leetcode237

Programming Language: python

Author: Dark dominate

E-mail: [email protected]

Leetcode- delete the list of nodes

Title Description

Original title link:

237. delete the list of nodes

19. reciprocal delete the list of N nodes

Subject description:

Here the subject description, only these two casual working title of written, specific, small partners after opening the link can view.

237. delete the list of nodes:
  Please write a function that makes it possible to delete a list given (non-end) node, you will only be required for a given node is removed.
  
19. Remove the list penultimate N nodes:
  Given a list, delete list penultimate n nodes, and returns the list of the first node.
  

Problem-solving ideas

237. delete the list of nodes

A list, it does not want an array that can be indexed elements we specify, so we delete a node in the list of methods, one is the node before the modification, the second is to do the exchange with the next node. , Shown below, is the difference between the two methods, to broken links red, blue indicates the redirected address.

Here Insert Picture Description

In this leetcode delete the list of nodes in the title, we only know the current node, a node does not know before, so we can only use the second method, the code is as follows:

class Solution:
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        node.val = node.next.val
        node.next = node.next.next

As seen in the code, just two lines. The first operation is to assign a value to a node of the current node, a second step is the next address to be all points to a modification (shown in Figure) to the next.

19. reciprocal delete the list of N nodes

The list can not be indexed, it can only traverse from front to back; very natural idea, let's again traverse the linked list, you can know his length length, then we have to delete the penultimate None, is the first traverse from front to back length-None, then delete nodes you can be friends. According to this idea, as follows

Here Insert Picture Description

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        if not head:return 
        new = ListNode(0)
        new.next = head
        
        tra = head
        cnt = 0
        while tra:
            cnt += 1
            tra = tra.next
        k = cnt - n
        cur = new
        while  k:
            cur = cur.next
            k -= 1
        cur.next = cur.next.next
        return new.next

The above method requires two traverse, a method is provided below a traverse, if we have two pointers, go first nstep, and then began to take the second pointer, when a finish of the first, second pointers just come to the penultimate none. As shown below:

Here Insert Picture Description

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        res = ListNode(0)
        res.next = head
        fast = res
        
        while n:
            fast = fast.next
            n -= 1
        
        slow = res
        while fast and fast.next:
            slow = slow.next
            fast = fast.next
        slow.next = slow.next.next
        return res.next

Note:This paper has the wrong place, welcome to hesitate to correct me! ! !

Guess you like

Origin blog.csdn.net/EngineerHe/article/details/97683825