LeetCode 203. Remove Linked List Elements

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjxxyz123/article/details/79980398

问题描述

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

Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
Return: 1 –> 2 –> 3 –> 4 –> 5

问题分析

  • 利用dummy 节点来避免可能删除头结点这种特殊操作
  • 利用递归来避免可能删除头结点这种特殊操作

代码实现

  • dummy 节点法
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode preNode = dummy;
        ListNode curNode = head;
        while (curNode != null) {
            ListNode nextNode = curNode.next;
            if (curNode.val == val) {
                preNode.next = nextNode;
            }else {
                preNode = curNode;
            }
            curNode = nextNode;
        }
        return dummy.next;
    }
  • 递归
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        ListNode resList = removeElements(head.next, val);
        if (head.val == val) {
            return resList;
        }else {
            head.next = resList;
            return head;
        }
    }

two problems about linked list

07-19

who can solve these two problems?try these if you have the knowledge of data structure for c++.solve it in c++,please. rnrnProblem 1 .rnImplement a sorting algorithm using a singly linked list. Thernalgorithm (known as the Insertion sort algorithm) works as follows:rn1. It starts with an empty linked list.rn2. When a first node is inserted to the list, the node is inserted at the head of the list.rn3. To insert a new node, the algorithm traverses the linked list until it finds a specificrnposition to insert the new node by comparing the new node’s data value to eachrnexisting node’s data value.rn4. Traverses all the elements of the linked list to print out the sorted values. [optional]rn5. Repeats steps 3 and 4 for each and every new node.rnImplement the insertion sort algorithm in C++ to ascendingly sort integers. Your programrnshould receive an arbitrary number of input integers one by one until you interrupt or stoprnyour program (by pressing Ctrl-C, for example). For each input integer, the program printsrnout the sorted list after inserting the value. The screen shot of your program may look like thernfollowing:rn$ a.outrn== Insertion sort by [your name] ==rn[Empty List]rnInput an integer: 5rnSorted List : 5rnInput an integer: 7rnSorted List: 5 7rnInput an integer: 2rnSorted List: 2 5 7rnInput an integer: 6rnSorted List: 2 5 6 7rnInput an integer: 3rnSorted List: 2 3 5 6 7rnAlso answer the following questions (as the comments at the end of your C++ program) :rn� Explain the input sequences that will result in the best and the worst algorithmicrnperformances, in terms of the number of comparisons required.rn� If there are N input values to be sorted, what is the best case complexity and what is thernworse case complexity, in terms of N ?rn rnrnProblem 2 rn Implement the doubly circular linked list for integers in C++. Provide the following DCLL operations as the method members of the DCLL class :rn1. Add a node at the head of the listrn2. Add a node at the tail of the list (i.e. at head->previous)rn3. Add a node after a specific n-th node (e.g. after the 4-th node). If such node does notrnexist, simply deny the operationrn4. Delete a node from the head of the listrn5. Delete a node from the tail of the listrn6. Delete a specific n-th node (e.g. delete the 5-th node). If such node does not exist, simplyrndeny the operation.rn7. Delete all the nodes that have a specific value (i.e. yes, there can be duplicate nodes)rn8. Determine if the list contain a particular value and how many duplicate nodes there arern9. Traverse the list in a forward direction (i.e. from head to tail)rn10. Traverse the list in a backward direction (i.e. from tail to head)rnCreate a user interface in the main() function that is similar to the singly linked list (sll.cc)rnexample given on the class web page. The user interface should provide the above DCLLrnoperations appearing in such order. After applying each operation, your DCLL must stillrnremain consistent, i.e. all the pointers point to where they are supposed to.rn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试