首页 > 其他分享 >Remove Linked List Elements

Remove Linked List Elements

时间:2023-07-22 14:23:21浏览次数:33  
标签:dummy Elements ListNode val int List next curr Linked

Source

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

Example
Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

题解

删除链表中指定值,找到其前一个节点即可,将 next 指向下一个节点即可。

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    /**
     * @param head a ListNode
     * @param val an integer
     * @return a ListNode
     */
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode curr = dummy;
        while (curr.next != null) {
            if (curr.next.val == val) {
                curr.next = curr.next.next;
continue; } curr = curr.next; }
return dummy.next; } }

源码分析

while 循环中使用 curr.next 较为方便,if 语句中比较时也使用 curr.next.val 也比较简洁,如果使用 curr 会比较难处理。

复杂度分析

无  

标签:dummy,Elements,ListNode,val,int,List,next,curr,Linked
From: https://www.cnblogs.com/lyc94620/p/17573303.html

相关文章

  • 模拟ArrayList(顺序表)的底层实现
    模拟ArrayLIst的底层实现packagecom.tedu.api04.list;importjava.util.Objects;/***@authorLIGENSEN*Date:2023/7/2011:35*/publicclassArrayListDemo{publicstaticvoidmain(String[]args){ArrList<String>list=newArrList<>......
  • struts2 jsonplugin includeProperties中对list集合的正则配置
     1、listAttachment.*\.realName 和listAttachment.* 效果一样,元素中所有的属性都json化了   "listAttachment":[{"encodedRealName":"%E8%AE%A1%E5%88%92%E4%BB%BB%E5%8A%A1%E4%B9%A6%E5%88%97%E8%A1%A820111223102409.xls","id":"408080b7......
  • plist切割器
    简单的小工具,用以提取plist图集中的小图片。限于个人精力和时间,工具暂只能拆解部分格式的plist图集(后续可能再作完善)。  ......
  • Could not get list of tables from database. Probably a JDBC driver problem.
     在用myeclipse8.5M1反向生成代码时报错: Aninternalerroroccurredduring:"GeneratingArtifacts".Couldnotgetlistoftablesfromdatabase.ProbablyaJDBCdriverproblem.  =============================  尝试了更换工作空间、重装myeclipse、更换oracle驱动......
  • Newtonsoft.Json 解析时 导致 List 增加的bug
    测试用例publicclassChild{publicinta=1;}publicclassParent{privateList<Child>_Children=newList<Child>(){newChild()};publicList<Child>Children{get......
  • list的用法
    0.display函数使用泛型,定义打印函数。template<typenameContainer>voiddisplay(Container&con){for(auto&elem:con){cout<<elem<<"";}cout<<endl;}1.初始化list<int>number={......
  • redis map内嵌list 重启取值失败
    RedisMap内嵌List重启取值失败解决方案1.问题描述在使用Redis过程中,有时候需要将一个List嵌套在Map中,但是当Redis服务重启后,再次取值时会出现获取不到嵌套在Map中的List的情况。这个问题可能是由于Redis服务重启后,Map中的数据没有正确地进行数据序列化和反序列化导致的。2.......
  • Android ListView去掉点击动效
    AndroidListView去掉点击动效作为一名经验丰富的开发者,我很高兴能够教会你如何实现“AndroidListView去掉点击动效”。下面我将为您提供详细的步骤和相应的代码。步骤步骤操作1.创建一个自定义的ListViewSelector文件2.在ListView布局中应用自定义的Selector文......
  • C# 将一个list集合部分字段加入另一个集合 Select使用
    SubmitUserInfoRequestDtoresponseDto=newSubmitUserInfoRequestDto(){id=item.NodeId,name=item.NodeName};varuserList=SMZX_ApprovalMember.GetList(item.N......
  • java list对象转字符串
    JavaList对象转字符串引言在Java开发中,经常会遇到需要将List对象转换为字符串的场景,例如将List中的数据展示在界面上或者将List中的数据保存到文件中。本文将介绍Java中如何实现List对象到字符串的转换。流程概述下表展示了将List对象转换为字符串的步骤及每个步骤需要做的事......