移除链表元素

此博客链接:https://www.cnblogs.com/ping2yingshi/p/12730036.html

移除链表元素(80min)

题目链接:https://leetcode-cn.com/problems/remove-linked-list-elements/submissions/

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

题解:

         思路:此题需要考虑5种情况。

                   情况1:链表为空时。返回null。

                   情况2:链表中只有一个数,但是和给定值不同,此时返回头节点。

                  情况3:链表中只有一个数,但是和给定值相同,此时返回null.

                  情况4:链表中不止一个数字,这里又分为两种情况:

                                                                                                                                           情况1:头节点和给定数字相同,此时让头节点指向next。

                                                                                                                                           情况:2:头节点和给定数字不相同,此时把头结点赋值给一个新的节点,当新的节点的next的值和给定值相同时,把新节点指向的next的next,不同时,新节点指向next。

错误情况1:没有考虑链表为空时,返回空。

错误情况2:没有考虑链表中元素不止一个,且和给定值都相等,此时返回空。

 错误代码:

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head==null)
            return null;
        if(head.val==val&&head.next==null)//第一个数相等,链表只有一个数,返回空
            return null;
        if(head.val!=val&&head.next==null)//第一个数不相等,但是链表中只有一个数,返回头结点
                return head;
        while(head!=null)
        {
            if(head.val==val&&head.next.next!=null&&head.next.val!=val)//第一个数相等,但是链表不止一个数,头节点指向下一个节点
            {
                 ListNode cur=head;
                head=head.next;
            }
            if(head.val==val&&head.next.next!=null&&head.next.val==val)//第一个数相等,但是链表不止一个数,头节点指向下一个节点
            {
                 ListNode cur=head.next;
                head=head.next.next;
            }
            if(head.val==val&&head.next.next==null&&head.next.val==val)//第一个数相等,但是链表不止一个数,头节点指向下一个节点
            {
              head=head.next.next;
              return head;
            }
               
            // if(head.val==val&&head.next==null)//第一个数相等,但是是最后一个节点
            // {
            //       head.next=null;
            //       return head;
            // }
             
             else {
                ListNode cur=head;
                 break;
        }
        }
        //if(head.val!=val&&head.next!=null)//第一个数不相等,链表中有多个数
            ListNode cur=head;
        while(cur.next!=null)
        {
            if(cur.next.val==val&&cur.next.next!=null)
               cur.next=cur.next.next;
            else if(cur.next.val==val&&cur.next.next==null)
               cur.next=null;
            else if(cur.next.val!=val&&cur.next.next==null)
                return head;
            else 
               cur=cur.next;
        }
         return head;
    }
}

 正确代码:

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head==null)
            return null;
        if(head.val==val&&head.next==null)//第一个数相等,链表只有一个数,返回空
            return null;
        if(head.val!=val&&head.next==null)//第一个数不相等,但是链表中只有一个数,返回头结点
                return head;
                ListNode cur=null;
        while(head!=null)
        {
            if(head.val==val)//第一个数相等,但是链表不止一个数,头节点指向下一个节点
            {
               
                head=head.next;
            }
             else {
                 cur=head;
                 break;
        }
        }
        //if(head.val!=val&&head.next!=null)//第一个数不相等,链表中有多个数
        
        while(cur!=null)
        {
            if(cur.next!=null&&cur.next.val==val)
               cur.next=cur.next.next;
            else 
               cur=cur.next;
        }
         return head;
    }
}
原文地址:https://www.cnblogs.com/ping2yingshi/p/12730036.html