class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode myHead = new ListNode(-1);
myHead.next = head;
ListNode pos1 = head;
ListNode pos2 = myHead;
//定位
while (pos1.val != val) {
pos2 = pos1;
pos1 = pos1.next;
}
//删除
pos2.next = pos1.next;
return myHead.next;
}
}
评论