思路:
因为只给当前要删除的节点的访问。所以我们没法返回之前的节点设置next。所以我们只能按照数组删除的方式,把之后所有的节点数据往前移动。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void deleteNode(ListNode node) {
ListNode next=node.next;
while(next.next!=null)
{
node.val=next.val;
node=next;
next=next.next;
}
node.val=next.val;
node.next=null;
}
}