讨论/《链表》 - 移除链表元素/
《链表》 - 移除链表元素
共 32 个回复

RESULT:
image.png

CODE:

    private ListNode removeElements(ListNode head, int val) {
        if (head == null) return null;
        ListNode index = head;
        // 如果当前节点的 next节点值和 val相同,则删除。如果不相同,继续执行下一个。
        while (index != null && index.next != null) {
            if (index.next.val == val) {
                index.next = index.next.next;
                continue;// 这里是关键,防止出现多个目标值时露删的情况
            }
            index = index.next;
        }
        // 最后处理头部
        if (head.val == val) {
            head = head.next;
        }
        return head;
    }

3

多多指教嘿咻嘿咻
class Solution {

代码块
public ListNode removeElements(ListNode head, int val) {
             if(head ==null){
                 return null;
             }
             //链表为空返回空
             ListNode pre = head;
             ListNode next=head.next;
             //从第二个节点开始看是否为空,如果不为空,分两种情况
             while (next!=null){
                 //若相等,跳过指向的元素到后面一个元素判断
                 if(next.val==val){
                     pre.next=next.next;
                      next=pre.next;
                 }
                 //若不相等,继续往后看
              else{
                  if(next.val!=val){
                     pre =pre.next;
                     next=next.next;
                  }
              }
              //因为是从第二个节点开始的,第一个也要看下哈                 
             }
              if(head.val==val){
                   head =head.next;
               }
               return head;


}

}

1

虽然在这里没必要,但是养成一个记得释放的好习惯

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* ans = new ListNode(-1,head), *ptr = ans, *deleted = nullptr;
        while(ptr && ptr->next){
            if(ptr->next->val == val){
                deleted = ptr->next;
                ptr->next = ptr->next->next;
                delete deleted;
            }
            else ptr = ptr->next;
        }
        deleted = ans;
        ans = ans->next;
        delete deleted;
        return ans;
    }
};
1

好不容易自己写对了一次,我也贴个😝

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null) {
            return null;
        }
        ListNode prev = null;
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == val) {
                if(prev == null) {
                    head = head.next;
                } else {
                    prev.next = cur.next;
                }
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
        return head;

    }
}
2

这是不是传递参数 是指向同个对象

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode newHead = new ListNode();
        ListNode tempHead = newHead;
        while (head != null) {
            if (head.val != val) {
                tempHead.next = head;
                tempHead = tempHead.next;
            } else {
                tempHead.next = null;
            }
            head = head.next;
        }
        return newHead.next;
    }
}

C++
两个问题
1.continue,一定要加,连续的相同节点,需要基于此节点反复操作,如果直接往下走会跳过这个字段
2.判空有顺序要求,while(index != NULL && index->next != NULL) 是对的,顺序反过来会报错

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(head == NULL) return NULL;
        ListNode *index = head;
        while(index != NULL && index->next != NULL)
        {
            if(index->next->val == val)
            {
                index->next = index->next->next;
                continue;//重要,删除节点后就已经完成了后移,后面语句跳过
            }
            index = index->next;
        }
        if(head->val == val)
        {
            head = head->next;
        }
        return head;
    }
};

记录前一节点并记录当前处理的节点:

public ListNode removeElements(ListNode head, int val) {
        ListNode pre = null, cur = head, h = head;
        while (cur != null) {
            if (cur.val == val) {
                if (h == cur) {
                    h = h.next;
                }else{
                    pre.next = cur.next;
                    cur=cur.next;
                    continue;
                }
            }
            pre = cur;
            cur = cur.next;
        }
        return h;
    }

一键三连加投币,简单一哥白手刷题,提高最直接有效的解法
1.在删除前记录头节点temp=head(处理完节点以后可以直接的返回)
2.然后用一个beforehead记录head节点的上一个位置,用于在head.val==val的时候删除head
3.删除一个节点以后head回溯一个节点head=beforehead然后一直删除到head==null
4.返回节点temp得到答案

public static ListNode removeElements(ListNode head, int val) {
        if(head==null){
            return null;
        }
        //用一个temp记录头节点
        ListNode temp=head;
        //先把头节点都去掉
        while(head!=null&&head.val==val){
            head=head.next;
            temp=head;
        }
        if(head==null||head.next==null){
            return head;
        }
        //用这个节点来记录head节点的前一个元素
        ListNode beforeHead=null;
        while(head!=null){
            beforeHead=head;
            head=head.next;
            if(head!=null&&head.val==val){
                beforeHead.next=beforeHead.next.next;
                //删除一个节点以后head上回一步
                head=beforeHead;
            }
        }
        return temp;
    }

321bd6dae80afc059d6db9e59230ff0.png

python3双指针

class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        while head and head.val == val: head = head.next # 若head.val = val则移动head直到其值不为val
        if not head: return head # 若head为空,直接返回
        pre = head
        cur = head.next
        while cur:
            if cur.val == val: # 当前节点cur.val = val
                cur = cur.next # 当前节点往右移动
                pre.next = cur # pre指向当前节点
            else: # 当前节点cur.val != val,同时右移双指针
                pre = pre.next
                cur = cur.next
        return head