- 浏览: 183707 次
- 性别:
- 来自: 济南
文章分类
最新评论
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
从链表中删除节点为给定值得所有节点。对于在链表中删除节点的问题,如果头结点可能被删除的情况下,我们往往创建一个helper节点,让它指向头结点。这样从helper节点的next开始处理,最后返回help.next就可以了。代码如下:
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
从链表中删除节点为给定值得所有节点。对于在链表中删除节点的问题,如果头结点可能被删除的情况下,我们往往创建一个helper节点,让它指向头结点。这样从helper节点的next开始处理,最后返回help.next就可以了。代码如下:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode removeElements(ListNode head, int val) { ListNode helper = new ListNode(0); helper.next = head; head = helper; while(head.next != null) { if(head.next.val == val) { head.next = head.next.next; } else { head = head.next; } } return helper.next; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 500Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 430Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 676Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 845Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 707For a undirected graph with tre ...
相关推荐
python python_leetcode题解之203_Remove_Linked_List_Elements.py
leetcode 2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 ...complexitypython.txt Python的一些常规操作的...Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List
27. **Remove Linked List Elements**:移除链表中的特定元素。可以使用迭代方法,同时保持指向下一个非目标节点的引用。 28. **Remove Element**:从数组中移除特定元素。双指针法可以有效地完成这个任务。 29. *...
Elements 力扣 206 反转链表 | Reverse Linked List 队列 Queue 力扣 933 最近的请求次数 | Number of Recent Calls 力扣 225 用队列实现栈 | Implement Stack Using Queue 力扣 622 设计循环队列 | Design ...
链表的基本类型包括单向链表(Singly Linked List)和双向链表(Doubly Linked List)。在单向链表中,每个节点只包含一个指向其后继节点的指针;而在双向链表中,除了包含指向后继节点的指针外,每个节点还包含一个...
关于algorithm and data structure的一个linked list的C++的code
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...
creat() linked list;print(),output linked list;insert(),input index, before data(index)insert new data,if index >c,insert into the end. delede(),delete data(index),0;change(),input index_i and index_...
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→...
用linked list来写priority queue
《Lazy Linked-List:一种高效的内存管理策略》 在计算机科学中,数据结构是构建算法的基础,而链表作为其中的一员,因其灵活的插入和删除操作,在许多应用场景中都有着广泛的应用。本文将深入探讨一种特殊的链表...
在给定的"double_linked_list.rar"文件中,包含了使用双向链表进行排序的实现。排序的目标是将链表中的元素按照数值大小进行排列。这里提到了两种排序算法:选择法和冒泡法。 1. **选择法排序**: 选择法排序是一...
We introduce a method to dynamically construct highly concurrent linked lists on modern graphics processors. Once constructed, these data structures can be used to implement a host of algorithms ...
相比之下,单链表(Linked List)是一种线性数据结构,其中每个节点包含数据和指向下一个节点的引用。单链表不支持随机访问,但插入和删除操作通常比数组更快,因为无需移动后续元素。 4. **单链表操作** 在C#中,...
this is a template design for doubly linked list. it is just a reference for one who want to study template programming.
"Lab10_doublylinkedlist_" 这个标题暗示我们讨论的是关于链表数据结构的一个实验或练习,具体是双链表(Doubly Linked List)。双链表是一种线性数据结构,每个节点不仅包含数据,还包含两个指针,分别指向其前一个...
infix to postfix using linked list
在linux环境下已测试,放心使用,这个是根据stanford cs library的linked list文章里的代码敲出来的
In the module entry point, create a linked list containing five struct birthday elements. Traverse the linked list and output its contents to the kernel log buffer. Invoke the dmesg command to ensure ...
"前端大厂最新面试题-linked-list.docx" 本文档主要涵盖了链表(Linked List)相关的知识点,旨在帮助读者更好地理解链表的实现、操作和应用。 1. 环形链表(Ring Linked List) 环形链表是一种特殊的链表结构,...