public class Test16 {
public class ListNode{
int val;
ListNode next;
public ListNode(int value,ListNode nextNode) {
val=value;
next=nextNode;
}
}
public ListNode deleteNode(ListNode head, int val) {
if (head.val == val) return head.next;
ListNode pre = head;
ListNode cur = head.next;
while (cur != null && cur.val != val){
pre = cur;
cur = cur.next;
}
if (cur != null){
pre.next = cur.next;
}
return head;
}
public ListNode deleteNode(ListNode head,ListNode pToBeDeleted) {
if(head==null||pToBeDeleted==null)
return head;
if(pToBeDeleted.next!=null) {
ListNode nextNode=pToBeDeleted.next;
pToBeDeleted.val=nextNode.val;
pToBeDeleted.next=nextNode.next;
nextNode=null;
}else if(head==pToBeDeleted) {
pToBeDeleted=null;
head=null;
}else {
ListNode preNode=head;
while(preNode.next!=pToBeDeleted && preNode!=null) {
preNode=preNode.next;
}
if(preNode==null) {
return head;
}
preNode.next=null;
pToBeDeleted=null;
}
return head;
}
public static void main(String[] args) {
}
}