public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
删除一个单链表里的某个指定的节点:
1.修改指针指向的对象
public static void deleteNodeV2(ListNode head, ListNode node) {
if(head == null || node == null) {
return;
}
while (head != null) {
if(head.next.val == node.val) {
head.next = head.next.next;
return;
}
head = head.next;
}
}
2.指针指向的对象不变,节点的值覆盖,需要被删除node不是尾节点
public static void deleteNode(ListNode node) {
if(node == null || node.next == null) {
return;
}
node.val = node.next.val;
node.next = node.next.next;
return;
}
---------------------
作者:翁正存
来源:CSDN
原文:https://blog.csdn.net/Wengzhengcun/article/details/87971694
版权声明:本文为博主原创文章,转载请附上博文链接!