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 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

分析

昨天中午收到一个好消息于是许久不能平静,反反复复这题一直错误,越改越混乱,毕竟逻辑已经在九霄云外了……

今早删了昨天的代码重新来过,一次通过。

我新设置了3个索引:

newHead,指向头部,用于最后返回
pre,指向移动中节点的前一个节点
cur,指向移动中的节点

我就不画图了……通过cur的不断循环,while内部判断cur的值是否与给定的val相等,分别作相应操作。最后pre和cur都移动到链表后面了,返回整个链表的重任就交给了newHead了。

还有一个关键问题是,上面的方法是从链表第二个节点开始与val作比较,那么第一个节点呢?昨天的思路现在想想真是太繁琐了,昨天是用了两个while循环,第二个就是上文说到,第一个用于判断链表头部的元素是否和val相等。

但是更好的方法是直接使用newHead。之前不是对链表第二个元素到最后一个元素做了判断嘛,返回的时候直接返回newHead或newHead的下一个元素不就好了?

代码

/**
* 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) {
        if (!head) return NULL;
        ListNode *pre = head, *cur = head->next, *newHead = pre;
        if (!cur) return pre->val == val ? NULL : pre;
        while (cur) {
            if (cur->val == val) pre->next = cur->next;
            else pre = pre->next;
            cur = cur->next;
        }
        if (newHead->val == val) return newHead->next;
        else return newHead;;
    }
};
时间: 2024-08-05 15:25:11

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 Credits:Special thanks to @mithmatt for adding this probl

LeetCode OJ :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 ps:这一题感觉没什么技巧可言,选取一个头指针,一个当前指针,一个前向指针.简单的链表操作而已,代码如下: 1 /**

[LintCode] Remove Linked List Elements 移除链表元素

Remove all elements from a linked list of integers that have value val. Have you met this question in a real interview? Example Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

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

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

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 ==