[LC]203题 Remove Linked List Elements (移除链表元素)(链表)

①英文题目

Remove all elements from a linked list of integers that have value val.

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

②中文题目

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

③思路

1、加入链表为空,那么直接返回head。

2、考虑6,5,4,3,2,1,val=6.

所以,一开始就要判断头结点的值是否等于val。所以考虑用while或者if来写此处逻辑。

3、考虑,6,6,6,5,4,3,2,1,val=6

当我删除第一个6之后,后面又来一个6,所以,这个需要不停循环,直到首个元素不再是6.

所以大致是

1 while(head!=null&&head.val==val){
2         head=head.next;
3 }

4、考虑  1->2->6->3->4->5->6, val = 6。

国际惯例,先让head赋给别的结点名,比如

1 ListNode pre=head;//以后有什么事情,就在pre上做操作。而要return全部时,就return head

判断,如果pre.next的val和pre.next.next的val相等,那就把原本的pre.next删掉,代码写起来就是让pre的next不再指向pre.next,而是指向pre.next.next,

这个逻辑需要用一个while循环来写,while里保证pre.next存在,即不等于null。代码写起来就是

1 while(pre.next!=null){
2        if(pre.next.val==pre.next.next.val)
3             pre.next==pre.next.next;   //看见一次next,就读作往后走了一个单位。
4        else
5             pre=pre.next;   //否则,让pre往后顺延一个单位
6 }

④总的代码如下:

 1 class Solution {
 2     public ListNode removeElements(ListNode head, int val) {
 3         while(head!=null&&head.val==val)
 4             head=head.next;     //只有当头结点的val不再等于val时,才走出循环
 5         if(head==null)
 6             return head;
 7         ListNode pre=head;
 8         while(pre.next!=null){
 9             if(pre.next.val==val)
10                 pre.next=pre.next.next;
11             else
12                 pre=pre.next;   //否则,让pre往后顺延1个单位
13
14         }
15         return head;
16     }
17 }

1、第一次见到连续两个.next;

2、几乎看见每一个链表题里,都会写一句把原本的头结点给赋给一个新声明的结点,比如ListNode pre=head;

当然,在这个赋值语句之前,先要保证head已经稳定了(比如本题总程序的3-4行就是在寻求一个稳定的head,不然,谁敢把不确定的head赋值给pre)。

原文地址:https://www.cnblogs.com/zf007/p/11577836.html

时间: 2024-08-09 16:17:09

[LC]203题 Remove Linked List Elements (移除链表元素)(链表)的相关文章

leetcode第203题-Remove Linked List Elements

题目要求:从一个链表中删除指定的元素. 分析:首先要考虑链表的非空问题,我想这应该是判断链表和数组问题最首先要考虑处理的问题.其次是如果前几个元素都是与指定元素相同,也要做一次处理,使head指向(在非空的情况下)与指定元素不相等的第一个元素.注意:在这里我碰到了一个很可笑的问题,就是while里面循环的判断条件,应该让head!=NULL在前面,这样就能保证head->val的值的问题,一直在这里出问题,后来才检查出来.然后再定义两个指针,第一个指针p指向当前的元素,第二个指针指向下一个节点,

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

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 题目要求: 删除链表中包含val的元素结点 解题思路: 重点在于找到第一个非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算法题-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 第一种解法 特殊情况

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