标签:head off while 链表 turn lse static 删除 int
题目:在O(1)时间删除链表节点
给定单向链表的一个头指针和节点指针,定义一个函数在O(1)时间删除该节点
public class Tenth { public class ListNode{ int val; ListNode next; } public static ListNode removeNode(ListNode head, ListNode toBeDeleted){ if(head == null || toBeDeleted == null){ return head; } //待删除节点为头节点 if(head == toBeDeleted){ return head.next; } //删除多个节点中的某个节点 ListNode pre = head; while(pre.next != toBeDeleted){ pre = pre.next; } //待删除的为尾节点 if(toBeDeleted.next == null){ pre.next = null; }else{ //待删除的节点在链表中 pre.next = toBeDeleted.next; toBeDeleted.next = null; } return head; } }
标签:head off while 链表 turn lse static 删除 int
原文地址:https://www.cnblogs.com/HarSong13/p/11325509.html