赞
踩
正常的链表删除是删除要删除的节点,而本题则是只知道当前要删除的节点,所以只能先将后一节点的值复制到当前结点,然后将后一节点当做待删除节点来进行常规删除。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
# 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。