leetcode 203 Remove Linked List Elements

简介:  Remove all elements from a linked list of integers that have valueval. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 ...


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

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



我的解法:




// Linklist.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include<iostream>
using namespace std;


  struct ListNode
  {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
  };
 


    ListNode* removeElements(ListNode* head, int val) 
    {
        if(head == NULL)return head;
        
        ListNode* pre = NULL;
        ListNode* root = head;
        ListNode* current = head;
        
        
        while(current!=NULL)
        {
            
            if(current->val == val)
            {
                if(pre==NULL)
                {
					current = current->next;
                    root = current;
                    

                }
                else
                {
                    pre->next = current->next;
                    current = current->next;
                    
                }
                
               
            }
            else
            {
                pre = current;
                current =current->next;
                
                
            }
        }
        
        return root;
    }

int _tmain(int argc, _TCHAR* argv[])
{
	ListNode* temp = new ListNode(2);
	ListNode* temp_next = new ListNode(1);
	temp->next = temp_next;
	removeElements(temp,1);
	return 0;
}



python的解法:


class Solution:
    # @param {ListNode} head
    # @param {integer} val
    # @return {ListNode}
    def removeElements(self, head, val):
        dummy = ListNode(-1)
        dummy.next = head

        prev = dummy
        while head:
            if head.val == val:
                prev.next = head.next
                head = prev
            prev = head
            head = head.next
        return dummy.next



一个非常简洁的解法:


struct ListNode* removeElements(struct ListNode* head, int val) 
{
    if (head&&head->val==val)head=removeElements(head->next, val);
    if (head&&head->next)head->next=removeElements(head->next, val);
    return head;
}



相关文章
|
6月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
21 1
|
6月前
|
C++
Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。
28 0
|
6月前
Leetcode 19.Remove Nth Node From End of List
删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
21 0
|
1月前
List中的remove方法遇到报错不能删除以及四种解决办法点赞收藏
List中的remove方法遇到报错不能删除以及四种解决办法点赞收藏
21 0
|
2月前
使用List中的remove方法遇到数组越界
使用List中的remove方法遇到数组越界
22 2
|
5月前
|
大数据 Java 程序员
「LeetCode合集」链表(List)及经典问题
「LeetCode合集」链表(List)及经典问题
28 0
|
6月前
避免list中remove导致ConcurrentModificationException
避免list中remove导致ConcurrentModificationException
20 0
|
11月前
for-each或迭代器中调用List的remove方法会抛出ConcurrentModificationException的原因
for-each循环遍历的实质是迭代器,使用迭代器的remove方法前必须调用一下next()方法,并且调用一次next()方法后是不允许多次调用remove方法的,为什么呢?接下来一起来看吧
55 0
List的remove操作一定要小心!
List的remove操作一定要小心!
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle