https://nowpick.nowcoder.com/w/msg#/recommend-conversation
菜鸟的两种解法
import java.util.*;
public class Solution {
public ListNode deleteNode (ListNode head, int val) {
// write code here
//方法一
ListNode pre=null,cur=head;
while(cur!=null){
if(cur.val==val){
if(cur==head)
return head.next;
pre.next = cur.next;
cur.next = null;
break;
}
pre = cur;
cur = cur.next;
}
return head;
//方法二
ListNode cur = head;
if(cur.val==val)
return cur.next;
cur = cur.next;
ListNode pre = head;
while(cur!=null){
if(cur.val==val){
pre.next = cur.next;
cur.next = null;
return head;
}
pre = cur;
cur = cur.next;
}
return head;
}
}