zoukankan      html  css  js  c++  java
  • 203. Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val.

    Example:

    Input:  1->2->6->3->4->5->6, val = 6
    Output: 1->2->3->4->5

    如果第一个数字,就需要删除,那么需要重置head。所以自己构建1个myHead。

    public ListNode RemoveElements(ListNode head, int val)
            {
                ListNode myHead = new ListNode(0);
                myHead.next = head;
                ListNode current = head;
                ListNode currentPrev = myHead;
                while (current != null)
                {
                    if (current.val == val)
                    {
                        current = current.next;
                        currentPrev.next = current;
                    }
                    else
                    {
                        currentPrev = current;
                        current = current.next;
                    }
                }
    
                return myHead.next;
            }

    Remove all elements from a linked list of integers that have value val.

    Example:

    Input:  1->2->6->3->4->5->6, val = 6
    Output: 1->2->3->4->5
  • 相关阅读:
    第52周二Restful
    第52周一
    第51周日
    第51周六
    第51周五
    第51周四
    第51周三
    第51周二
    第51周一
    第50周日
  • 原文地址:https://www.cnblogs.com/chucklu/p/10504943.html
Copyright © 2011-2022 走看看