1. 程式人生 > >LeetCode題目-- 刪除連結串列中的節點(python實現)

LeetCode題目-- 刪除連結串列中的節點(python實現)

題目

請編寫一個函式,使其可以刪除某個連結串列中給定的(非末尾)節點,你將只被給定要求被刪除的節點。

現有一個連結串列 -- head = [4,5,1,9],它可以表示為:

    4 -> 5 -> 1 -> 9

示例 1:

輸入: head = [4,5,1,9], node = 5
輸出: [4,1,9]
解釋: 給定你連結串列中值為 5 的第二個節點,那麼在呼叫了你的函式之後,該連結串列應變為 4 -> 1 -> 9.

示例 2:

輸入: head = [4,5,1,9], node = 1
輸出: [4,5,9]
解釋: 給定你連結串列中值為 1 的第三個節點,那麼在呼叫了你的函式之後,該連結串列應變為 4 -> 5 -> 9.

說明:

  • 連結串列至少包含兩個節點。
  • 連結串列中所有節點的值都是唯一的。
  • 給定的節點為非末尾節點並且一定是連結串列中的一個有效節點。
  • 不要從你的函式中返回任何結果。

python程式碼實現:

 

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

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
        

注:

連結串列的初始化操作:

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

class Solution():
    
    def __init__(self,node):

        self.head=Node(node)
        self.head.next=None

    def add(self, node):
        node.next=self.head.next
        self.head.next=node

    def removeNthFromEnd(self,head,n):

        new_head = Node(0)
        new_head.next = head
        fast = slow = new_head
        for i in range(n + 1):
            fast = fast.next
        while fast:
            fast = fast.next
            slow = slow.next
        slow.next = slow.next.next
        return new_head.next
  
  
    def isPalindrome(self, head):
        if head == None or head.next == None:
            return True
    #利用快慢指標法尋找連結串列的中點
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        # slow = slow.next
        slow = self.reverseList(slow)
    #前半段與後半段進行對比
        while slow:
            if head.val != slow.val:
                return False
            slow = slow.next
            head = head.next
        return True
    #利用頭插法反轉後半段連結串列
    def reverseList(self, head):
        new_head = None
        while head:
            p = head
            head = head.next
            p.next = new_head
            new_head = p
        return new_head

    def hasCycle(self, head):
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True
        return False








def main():

   
    node2 = Node(1)
    node3 = Node(2)
    node4 = Node(3)
    node5 = Node(4)
    node6 = Node(5)
    node7 = Node(6)
    node8 = Node(7)
    linklist = Solution(0)
    linklist2 = Solution(0)
    linklist.add(node2)
    linklist.add(node3)
    linklist.add(node4)
    linklist.add(node6)
    linklist.add(node8)
    linklist2.add(node2)
    linklist2.add(node3)
    linklist2.add(node5)
    # i=1
    # while linklist.head is not None:
    #     # i = +1
    #     # print(i)
    #     linklist.head=linklist.head.next
    # linklist.print_list()
    t=Solution(1)
    # nu=t.mergeTwoLists(linklist.head,linklist2.head)
    nu = t.isPalindrome(linklist.head)
    # while nu is not None:
    #     # i = +1
    #     # print(i)
    #     print(nu.val)
    #     nu=nu.next
    print(nu)
    
if __name__ == '__main__':
    main()