一. 链表 3 删除节点

问题:给定一个单链表中的一个等待被删除的节点(非表头或表尾)。请在在O(1)时间复杂度删除该链表节点。

思路:因为题目只给了一个待删除节点,所以,我们不能使用遍历。
我们只需要将当前节点的值被下一个节点的值覆盖就好。注意,末尾的节点没有值可以覆盖,所以我们要单独处理它。即,将last node 的next指针指向next node。完美地忽略它。

Python3

"""
Definition of ListNode
class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""
class Solution:
    # @param node: the node in the list should be deleted
    # @return: nothing
    def deleteNode(self, node):
        # create a node to store the last node of current node
        last_node = node
        # when the current node is not null 
        while node != None:
            # when the next node is null, then delete the current node 
            if node.next == None:
                last_node.next = node.next
                break
            # use the next node to replace the current node 
            node.val = node.next.val
            # regard the current node as the last node 
            last_node = node
            # jump to the next node 
            node = node.next

其实在处理最后一个尾节点上,我有想过要直接删除该点,但是在python中都是引用。所以,del语句作用在变量上,而不是数据对象上。也就是说,当我把一个变量名删除,我只是删除了一个引用,数据(节点)还在,依旧可以被找到。

Java

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param node: the node in the list should be deleted
     * @return: nothing
     */
    public void deleteNode(ListNode node) {
        // create a node to store the last node of current node
        ListNode last_node = node;
        // when the current node is not null 
        while(node != null)
        {
            //  when the next node is null, then delete the current node
            if (node.next == null)
            {
                last_node.next = node.next;
                break;
            }
            // use the next node to replace the current node
            node.val = node.next.val;
            // regard the current node as the last node 
            last_node = node;
            // jump to the next node 
            node = node.next;
        }
    }
}

在java的算法里,我也尝试了直接删除尾节点,即 node = null;等待java的垃圾回收机制进行回收。
但是效果欠佳,我想主要是因为这个回收机制是有一定的时间触发和一定的触发条件,所以在短暂的时间内,它做不到垃圾回收的。

最后
我用python 的运行时间(编译时间+执行时间) 比java的少了一个数量级。但是普遍认识是Java的效率更高,矛盾?

其实,有人解释的好,“Python是解释型的,不用虚拟机。所以启动快。就好比你是跑步的,别人是起自行车的,你直接起步,别人还得先打开车锁,再开始骑。但是你起步快半分钟又有何用,如果是去马路对面那是你快,如果是超过500米以上呢。~~~~~至于C++,那是随时待命的导弹,先考虑你能否驾驭好他吧。”

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • //leetcode中还有花样链表题,这里几个例子,冰山一角 求单链表中结点的个数----时间复杂度O(n)这是最...
    暗黑破坏球嘿哈阅读 5,420评论 0 6
  • 一、基本数据类型 注释 单行注释:// 区域注释:/* */ 文档注释:/** */ 数值 对于byte类型而言...
    龙猫小爷阅读 9,718评论 0 16
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,353评论 0 33
  • 这个月的最后一天。 我来到山东的第一个早晨。 5点半起床,蹑手蹑脚的穿衣服。这里的温度果然和东北不一样,寝室穿半袖...
    丶行烟阅读 1,326评论 0 2
  • 背景 2017年《中国有嘻哈》力压一众选秀节目火遍中国,这种在国内不为大众所了解的音乐在节目一播出之后得到了爆炸式...
    ThePromise_bed9阅读 3,529评论 0 0