Leetcode list の simple questions training set (237 404 876) python

Disclaimer: This article is original, All Rights Reserved https://blog.csdn.net/weixin_41864878/article/details/91040181

237. delete the list of nodes

Here Insert Picture Description
Here is the list node structure itself, only the value of the next node and the node pointer assigned to the line, the topic was relatively subtle bar

class Solution(object):
    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

707. Design list

Implement the design list. You can choose to use single or double-linked list. Single node in the list should have two attributes: val and next. and val is the value of the current node, next pointing to the next node pointer / reference. To use a doubly linked list, an attribute is also required to prev one node indicating the list. Suppose all the nodes in the list are the 0-index.

Implementing these features in the list class:

get (index): Gets the value of the index in the list of nodes. If the index is invalid, -1 is returned.
addAtHead (val): adding a node to val before the first element in the list. After insertion, the new node will be the first node of the list.
addAtTail (val): the node is added to the value val is the last element of the list. addAtIndex (index, val): the first in the linked list
to add a node to val before index nodes. If the index is equal to the length of the list, then the node will be appended to the end of the list. If the index
is greater than the length of the list, the node will not be inserted. deleteAtIndex (index): If the index index is valid, delete the list of index nodes.

Example:

= New new MyLinkedList linkedList MyLinkedList (); linkedList.addAtHead (. 1);
linkedList.addAtTail (. 3); linkedList.addAtIndex (1,2); // list becomes l-> 2->. 3
linkedList.get (. 1); // returns linkedList.deleteAtIndex 2 (. 1);
// list is now 1-> 3 linkedList.get (1); // returns 3

prompt:

All values ​​are [1, 1000] within. The number of operations in [1, 1000] within. Do not use the built-in LinkedList library.

The question really sand sculpture, first in python can achieve only a list, I do not write code self.head self.tail are ok and the
next, the title says the index from zero, but there is a test case index = -1 when inserted successfully
put people was very irritable

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class MyLinkedList(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.head = ListNode(None)
        self.val_list = []
        self.tail = self.head

    def get(self, index):
        """
        Get the value of the index-th node in the linked list. If the index is invalid, return -1.
        :type index: int
        :rtype: int
        """
        if index >= 0 and index <= len(self.val_list)-1: #这里一定要做index为正值的限定!
            return self.val_list[index]
        else:
            return -1
        
    def addAtHead(self, val):
        """
        Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
        :type val: int
        :rtype: None
        """
        head = ListNode(val)
        head.next = self.head
        self.head = head
        self.val_list.insert(0, val)

    def addAtTail(self, val):
        """
        Append a node of value val to the last element of the linked list.
        :type val: int
        :rtype: None
        """
        tail = ListNode(val)
        self.tail.next = tail
        self.tail = tail
        self.val_list.append(val)
        
    def addAtIndex(self, index, val):
        """
        Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
        :type index: int
        :type val: int
        :rtype: None
        """
        if index == len(self.val_list): self.addAtTail(val)
        elif index == -1: self.addAtHead(val) #沙雕的index = -1时的插入
        elif index < len(self.val_list) and index >= 0: #必须做index为正值的限定
            node = self.head
            steps = 0
            while index - 1 > steps:
                node = node.next
                steps += 1
            tmp = node.next
            new = ListNode(val)
            node.next = new
            new.next = tmp
            self.val_list.insert(index, val)

    def deleteAtIndex(self, index):
        """
        Delete the index-th node in the linked list, if the index is valid.
        :type index: int
        :rtype: None
        """
        if index < len(self.val_list) and index >= 0:
            node = self.head
            steps = 0
            while index-1 > steps:
                node = node.next
                steps += 1
            rem = node.next
            node.next = rem.next
            self.val_list.pop(index)

# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)

876. The intermediate node list

Given a non-empty list with a single head of the first node, an intermediate node list returned.

If there are two intermediate nodes, the second intermediate node is returned.

Example 1:

Input: [1,2,3,4,5] Output: node 3 (SEQ form: [3,4,5]) returns the list of nodes is 3.
(This evaluation system is expression of the sequence node [3,4,5]). Note that, we return an object of type ListNode ANS, so: = ans.val
. 3, ans.next.val =. 4, ans.next.next.val =. 5, and = ans.next.next.next
. Example NULL 2:

Input: [1,2,3,4,5,6] Output: this list node 4 (SEQ form: [4,5,6]) Since the list has two intermediate nodes, values of 3 and
4, we return to the second node.

prompt:

Given the list of nodes is between 1 and 100.

Speed ​​pointer directly

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow = head
        fast = head.next
        while fast:
            slow = slow.next
            if fast.next:
                fast = fast.next.next
            else: 
                fast = fast.next
        return slow  

Guess you like

Origin blog.csdn.net/weixin_41864878/article/details/91040181