(237)-(Delete Node in a Linked List )-(给出待删除的节点,把他变成他后面的)-(理解题意以及停止点的细心)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
//我有点看不懂题目
//删除链表的一个节点,但只给你需要删除的那个节点。
//Write a function to delete a node (except the tail)
//in a singly linked list, given only access to that node.
//1 2 3 4 5 6
//要删除3
//3变成4
//4变成5
//5变成6
//6变成空
//1 2 4 5 6 空
public class Solution
{
public void deleteNode(ListNode node)
{
if(node == null||node.next == null)
{
return ;
}
while(node.next.next!=null)
{
node.val=node.next.val;
node=node.next;
}
//仍然要对最后一个赋值
node.val=node.next.val;
node.next=null;
return ;
}
}