【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

题解:

  这道题没什么好讲的,基础操作。需要注意的是链表的第一个node,因为没有前驱节点,所以该node需要特殊处理,会导致额外的代码量。如果创建一个dummy,将其作为第一个node的前驱节点,这样链表中所有的node都可以也能够同样的逻辑来处理了。

Solution 1 ()

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        static ListNode dummy(-1);
        dummy.next = head;
        ListNode *p = &dummy;

        while( p->next) {
            if (p->next->val == val) {
                p->next = p->next->next;
            }else{
                p = p->next;
            }
        }

        return dummy.next;
    }
};

  这里有个技巧,利用二级指针,操作简洁

Solution 2 ()

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

  还有一种递归的思想

Solution 3 ()

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if (head == null) return null;
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;
    }
};

最后附上创建链表和打印链表的函数

CreatList

ListNode* createList(vector<int> nums) {
    ListNode *head = NULL, *p = NULL;
    int n = nums.size();
    for(int i=0; i<n; ++i) {
        if(head == NULL) head = p = new ListNode(nums[i]);
        else {
            p->next = new ListNode(nums[i]);
            p = p->next;
        }
    }
    return head;
}

PrintList

void printList(ListNode *head) {
    ListNode *p = head;
    while(p) {
        if(p == head && p) {
            cout << p->val;
            p = p->next;
        }else {
            cout  << "->" << p->val;
            p = p->next;
        }
    }
    delete p;
    cout<<endl;
}
时间: 2024-05-09 22:57:01

【LeetCode】203. Remove Linked List Elements的相关文章

【leetcode?python】 203. Remove Linked List Elements

# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = None class Solution(object):    def removeElements(self, head, val):        """        :type head: ListNode

leetCode 203. Remove Linked List Elements 链表

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 题目大意: 删除链表中全部的目标元素. 代码如下:

203. Remove Linked List Elements - LeetCode

Question 203.?Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 Java实现: public ListNode removeElements(ListNode head, int val) { ListNode cur = head; while (cur != null) { if (cur.next != null && cur.next.val ==

LeetCode算法题-Remove Linked List Elements(Java实现)

这是悦乐书的第189次更新,第191篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第48题(顺位题号是203).移除单链表中节点值为val的节点.例如: 输入:1-> 2-> 6-> 3-> 4-> 5-> 6,val = 6 输出:1-> 2-> 3-> 4-> 5 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 特殊情况

Java [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 解题思路: 链表操作. 代码如下: /** * Definition for singly-linked

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 203. Remove Linked List Elements JAVA语言

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.  * 

Java for 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 解题思路: JAVA实现如下: public ListNode removeElements(ListNode h

[刷题] LeetCode 203 Remove Linked List Elements

要求 在链表中删除值为val的所有节点 示例 如 1->2->3->4->5->6->NULL,要求删除值为6的节点 返回1->2->3->4->5->NULL 思路 删除一般元素(包括最后一个元素) 删除第一个元素 实现 常规思路 1 #include <iostream> 2 using namespace std; 3 4 struct ListNode { 5 int val; 6 ListNode *next; 7 L