问题原文地址点击打开链接
对链表最基础的处理。代码已写注释。代码如下
public ListNode removeElements(ListNode head, int val) {
if (head == null) return head;
ListNode current = head;//移动的指针
ListNode pre = null;//用该变量记录上一个节点
while(current.next != null){
if (current.val == val){
//对特殊位置,头指针进行判断
if (current == head){
head = head.next;
current = head;
}else{
//当符合条件的点在链表中间部分时
ListNode next = current.next;
pre.next = next;//将该节点删除,并将该节点的前后两个节点连接
current = next;
}
}else{
//移动指针
pre = current;
current = current.next;
}
}
if (current.val == val){
//对末尾位置进行判断
if (pre == null) return null;
else pre.next = null;
}
return head;
}