• 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

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode(int x) { val = x; }
     7  * }
     8  */
     9 public class Solution {
    10     public ListNode removeElements(ListNode head, int val) {
    11         ListNode header = new ListNode(-1);
    12         header.next = head;
    13         ListNode pre = header;
    14         ListNode cur = head;
    15         while(cur != null){
    16             if(cur.val == val){
    17                 pre.next = cur.next;
    18             }else{
    19                 pre = pre.next;
    20             }
    21             cur = cur.next;
    22         }
    23         return header.next;
    24     }
    25 }
  • 相关阅读:
    记一次mqtt压测过程
    记项目过程中代码分支管理
    测试流程
    Docker与K8s的区别
    Mysql之pymysql
    Mysql常用简介
    JQuery
    CSS
    红外线接受程序 理解
    数码管流水灯升级程序理解
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/4465121.html
Copyright © 2020-2023  润新知