[LeetCode]203.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

思路

链表节点的删除操作。

代码

/*---------------------------------------
*   日期:2015-05-01
*   作者:SJF0115
*   题目: 203.Remove Linked List Elements
*   网址:https://leetcode.com/problems/remove-linked-list-elements/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

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

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(head == nullptr){
            return nullptr;
        }//if
        ListNode *dummy = new ListNode(0);
        dummy->next = head;
        ListNode* pre = dummy;
        ListNode* cur = head;
        ListNode* tmp;
        while(cur){
            if(cur->val == val){
                tmp = cur;
                pre->next = cur->next;
                delete tmp;
                cur = pre->next;
            }//if
            else{
                pre = cur;
                cur = cur->next;
            }//else
        }//while
        return dummy->next;
    }
};

int main() {
    Solution solution;
    ListNode* head = new ListNode(1);
    ListNode* node1 = new ListNode(5);
    ListNode* node2 = new ListNode(2);
    ListNode* node3 = new ListNode(1);
    ListNode* node4 = new ListNode(4);
    ListNode* node5 = new ListNode(1);
    head->next = node1;
    node1->next = node2;
    node2->next = node3;
    node3->next = node4;
    node4->next = node5;

    head = solution.removeElements(head,6);
    while(head){
        cout<<head->val<<endl;
        head = head->next;
    }//while
}

运行时间

时间: 2024-09-01 21:01:33

[LeetCode]203.Remove Linked List Elements的相关文章

[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 解题思路 定义两个指针pre和cur,如果cur的值为val,则删除该结点.需要注意的情况有两种:①需要删除头结点:②链表为空. 实现

[LeetCode]19.Remove Nth Node From End of List

[题目] Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: G

[LeetCode]82.Remove Duplicates from Sorted List II

[题目] Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2-

[LeetCode]27.Remove Element

[题目] Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. [题意] 把数组中与给定值相同的元素删除,在原数组上修改,返回值是最终元素个数. [分析] 无 [代码]

LeetCode 27 Remove Element(移除元素)

翻译 给定一个数组和一个值,删除该值的所有实例,并返回新的长度. 元素的顺序可以被改变,也不关心最终的数组长度. 原文 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new lengt

LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)

翻译 给定一个已排序的数组,删除重复的元素,这样每个元素只出现一次,并且返回新的数组长度. 不允许为另一个数组使用额外的空间,你必须就地以常量空间执行这个操作. 例如, 给定输入数组为 [1,1,2] 你的函数应该返回length = 2, 其前两个元素分别是1和2.它不关心你离开后的新长度. 原文 Given a sorted array, remove the duplicates in place such that each element appear only once and re

[LeetCode]234.Palindrome Linked List

题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 思路 利用双指针法找到链表中点位置,链表中点以后的的元素(不包括中点元素)翻转,再跟链表中点位置以前的元素一一匹配. 代码 /*--------------------------------------- * 日期:2015-07-18 * 作者:SJF01

LeetCode 19 Remove Nth Node From End of List(从列表尾部删除第N个结点)(*)

翻译 给定一个链表,移除从尾部起的第n个结点,并且返回它的头结点. 例如,给定链表:1->2->3->4->5,n = 2. 在移除尾部起第二个结点后,链表将变成:1->2->3->5. 备注: 给定的n是有效的,代码尽量一次通过. 原文 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list:

leetcode 26 Remove Duplicates from Sorted Array

Remove Duplicates from Sorted ArrayTotal Accepted: 66627 Total Submissions: 212739 My Submissions                       Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length. Do not allo