203、移除链表元素

 

 

 

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val){//如果头结点是要删除的值,就让头结点指向下一个节点
            head = head.next;
        }
        if (head == null){
            return head;
        }
         ListNode pre = head;
         ListNode cur = head.next;
         while (cur != null) {
            if (cur.val == val){
                pre.next = cur.next;
            }
            else{
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}

 

上一篇:力扣 203. 移除链表元素 难度:简单


下一篇:leetcode【链表—简单】203.移除链表元素