【leetcode】Remove Linked List Elements(easy)

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

思路:简单题。

//Definition for singly-linked list.
 struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
 };

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode fakeHead = ListNode(0); //伪头部,化简代码
        fakeHead.next = head;
        ListNode *p = &fakeHead;
        while(NULL != p->next)
        {
            if(p->next->val == val) //当前数字的下一个需要被删除 删掉后重新判断当前位置的下一个数
                p->next = p->next->next;
            else                    //当前位置的下一个不需要删除,把当前位置后移
                p = p->next;
        }
        return fakeHead.next;
    }
};
时间: 2024-08-25 23:37:45

【leetcode】Remove Linked List Elements(easy)的相关文章

【leetcode】Reverse Linked List II (middle)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. 思路: 好困啊,脑子晕晕的. 转了半天AC了.但写的很罗嗦,要学习大神的写法. 注意翻转的写法. 用伪头部 大神14行简洁代码 L

【leetcode】Merge Two Sorted Lists(easy)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路:使用伪头部 class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode fakehead(0)

【leetcode】Contains Duplicate & Rectangle Area(easy)

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 思路:简单题用set bool cont

leetcode_203题——Remove Linked List Elements(链表)

Remove Linked List Elements Total Accepted: 8053 Total Submissions: 29898My Submissions Question Solution Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val =

LeetCode【203】Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 很简单吧,没什么好说的.直接上AC代码: ListNode* removeElements(ListNode* hea

LeetCode 203 Remove Linked List Elements(移除链表元素)(*)

翻译 从一个链表中移除所有值为val的元素. 例如 给定:1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 返回:1 --> 2 --> 3 --> 4 --> 5 原文 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 -

【leetcode】Merge k Sorted Lists (归并排序)

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解析:合并k个有序链表,最后返回一个总的有序链表,分析并描述其复杂度.该题的实质为归并排序,平均时间复杂度为O(NlogN). Java AC代码: public class Solution { public ListNode mergeKLists(List<ListNode> list

【leetcode】Reverse Nodes in k-Group (hard)☆

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

【leetcode】Container With Most Water(middle)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe