小言_互联网的博客

9.23 练手 腾讯50题 237 删除链表

306人阅读  评论(0)

Leetcode 237. Delete Node in a Linked List

给一个节点,在链表中删除这个节点,不返回任何值。

代码如下:

class Solution(object):
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        pre = node
        while(node.next != None):
            node.val = node.next.val
            pre = node
            node = node.next
        pre.next = None

 

别人的代码如此简洁快捷:

class Solution(object):
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        temp = node.next
        node.val = node.next.val
        node.next = node.next.next
        temp = None

 


转载:https://blog.csdn.net/vancooler/article/details/101228527
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场