[LeetCode] Remove Linked List Elements

简介: 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

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

解题思路

定义两个指针pre和cur,如果cur的值为val,则删除该结点。需要注意的情况有两种:①需要删除头结点;②链表为空。

实现代码[C++]

//Runtime:38ms
#include <iostream>
using namespace std;

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

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        while (head != NULL && head->val == val)
        {
            head = head->next;
        }

        if (head == NULL)
        {
            return NULL;
        }
        ListNode *pre = head;
        ListNode *cur = head->next;
        while(cur != NULL)
        {
            if (cur->val == val)
            {
                pre->next = pre->next->next;
            }
            else
            {
                pre = pre->next;
            }
            cur = cur->next;
        }

        return head;
    }
};

void print(ListNode *head)
{
    while (head != NULL)
    {
        cout<<head->val;
        head = head->next;
    }
    cout<<endl;
}

int main()
{
    Solution s;
    ListNode *node1 = new ListNode(1);
    ListNode *node2 = new ListNode(2);
    ListNode *node3 = new ListNode(1);
    ListNode *node4 = new ListNode(2);
    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    print(node1);
    node1 = s.removeElements(node1, 2);
    print(node1);
}

实现代码[Python]

# Rumtime:181ms
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    # @param {ListNode} head
    # @param {integer} val
    # @return {ListNode}
    def removeElements(self, head, val):
        while head != None and head.val == val:
            head = head.next
        if head == None:
            return None
        pre = head
        cur = head.next
        while cur != None:
            if cur.val == val:
                pre.next = pre.next.next
            else:
                pre = pre.next
            cur = cur.next

        return head

def printList(head):
    # @param {ListNode} head
    while head != None:
        print(head.val, end = ' ')
        head = head.next
    print() # print '\n'

node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(1)
node4 = ListNode(2)
node1.next = node2
node2.next = node3
node3.next = node4

printList(node1) # print the origin list
s = Solution();
node1 = s.removeElements(node1, 1)
printList(node1) # print the result list
目录
相关文章
|
11月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
36 1
|
11月前
Leetcode 19.Remove Nth Node From End of List
删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
33 0
|
5月前
使用List中的remove方法遇到数组越界
使用List中的remove方法遇到数组越界
79 2
|
5月前
List中的remove方法遇到报错不能删除以及四种解决办法点赞收藏
List中的remove方法遇到报错不能删除以及四种解决办法点赞收藏
180 0
|
5月前
|
XML 存储 JavaScript
关于 SAP Fiori Elements List Report 里的 TableCell.fragment.xml 头部声明
关于 SAP Fiori Elements List Report 里的 TableCell.fragment.xml 头部声明
|
5月前
|
设计模式 前端开发 JavaScript
关于 SAP Fiori Elements List Report Go 按钮的实现
关于 SAP Fiori Elements List Report Go 按钮的实现
|
5月前
|
大数据 Java 程序员
「LeetCode合集」链表(List)及经典问题
「LeetCode合集」链表(List)及经典问题
46 0
|
5月前
|
SQL JSON Java
Java【问题记录 02】对象封装+固定排序+list All elements are null引起的异常处理+Missing artifact com.sun:tools:jar:1.8.0
Java【问题记录 02】对象封装+固定排序+list All elements are null引起的异常处理+Missing artifact com.sun:tools:jar:1.8.0
67 0
|
11月前
|
前端开发 开发者 容器
SAP Fiori Elements List Report 应用里 Header 字段的绑定路径
SAP Fiori Elements List Report 应用里 Header 字段的绑定路径
|
11月前
避免list中remove导致ConcurrentModificationException
避免list中remove导致ConcurrentModificationException
37 0