zoukankan      html  css  js  c++  java
  • 5.<Important> Delete Node in a Linked List

    Title:

    Write a function to delete a node (except the tail) in a singly linked list(单链表), given only access to that node.

    Given linked list -- head = [4,5,1,9], which looks like following:

    Example 1:

    Input: head = [4,5,1,9], node = 5
    Output: [4,1,9]
    Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.

    Example 2:

    Input: head = [4,5,1,9], node = 1
    Output: [4,5,9]
    Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.

    Note:

    • The linked list will have at least two elements.
    • All of the nodes' values will be unique.
    • The given node will not be the tail and it will always be a valid node(有效节点) of the linked list.
    • Do not return anything from your function.  !!!!!

    Analysis of Title:

    We needn't to analysis the title but the Note end that Do not return anything from your function.  

    Test case:

    [4,5,1,9]
    5

    Python:

    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

    Analysis of Code:

    This is the content of the linked list. 

    The algorithm is: 

      1.[4,5,1,9]-->>[4,1,1,9]

      2.[4,1,1,9]-->>[4,1,9]

    Notice: node.next means ponit next node, just like 5->1, so node.next.next means 5->->9.

     .next is a 'point', not a certain value, Originally I thought node.next is 1, so node.next = node.next.next means 5=9, it's wrong.

  • 相关阅读:
    sql语句清除mssql日志
    常用的一些webshell木马官方后门
    32个触发事件XSS语句的总结
    XSS的高级利用部分总结 -蠕虫
    IIS7 Appcmd.exe 使用
    PHP Execute Command Bypass Disable_functions
    仿5173游戏交易平台系统SQL注入(可直接脱裤)+Getshell
    win95+ie3-win10+ie11 浏览器执行漏洞
    SQL 、NoSQL数据库教程
    Pytorch快速入门及在线体验
  • 原文地址:https://www.cnblogs.com/sxuer/p/10628971.html
Copyright © 2011-2022 走看看