1.10 Given a particular node deletes the node pointer claim

The node to delete only the single case for a pointer to a linked list of nodes

Subject description:

Suppose a given list 1-> 2-> 3-> 4-> 5-> 6-> 7, pointer to the first element 5, 5 requires the node to delete, delete the list becomes 1-> 2- > 3-> 4-> 6-> 7

Analysis of ideas:

  1. If this node is the last node of the list, you can not delete this node.
  2. If the last node is not the node linked list, by copying the data to its successor node in the current node, then delete the successor node of methods.

    Code:

    # --coding:utf-8--
    """
    @Author : 图南
    @Software: PyCharm
    @Time : 2019/9/7 19:46
    """
    class Node:
    def init(self, data=None, next=None):
    self.data = data
    self.next = next

    def printLink(head):
    if head is None or head.next is None:
    return
    cur = head.next
    while cur != None:
    print(cur.data, end=" ")
    cur = cur.next
    print()

    def conLink(nums, n):
    nums = list(map(int, nums.split(' ')))
    n = int(n)
    if len(nums) == 0 or n == 0:
    return
    p = None
    head = Node()
    cur = head
    for i in range(1, len(nums)+1):
    node = Node(nums[i-1])
    cur.next = node
    cur = node
    if i == n:
    p = cur
    return head, p

    def deleteP(p):
    if p.next is None:
    return False
    p.data = p.next.data
    p.next = p.next.next
    return True

    IF name == ' main ':
    the nums = INPUT ( 'list:')
    n-INPUT = ( 'nodes:')
    head, P = conLink (the nums, n-)
    Print ( 'before deleting:')
    PrintLink (head)
    F deleteP = (the p-)
    IF f:
    Print ( 'after deleting:')
    PrintLink (head)
    the else:
    Print ( 'can not be deleted!')

    operation result:


Guess you like

Origin www.cnblogs.com/miao-study/p/11482889.html