• 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
  • 相关阅读:
    Python day 34 并发编程、PID/PPID、实现多进程得两种方式
    Python Day33:粘包问题及粘包解决方案
    数据分析
    数据分析
    爬虫 之 mongodb数据库
    爬虫
    爬虫
    爬虫
    flask框架
    flask框架
  • 原文地址:https://www.cnblogs.com/chucklu/p/10504943.html
Copyright © 2020-2023  润新知