首页 > 其他分享 >【leetcode】【83】【移除链表元素】

【leetcode】【83】【移除链表元素】

时间:2023-07-01 12:22:05浏览次数:29  
标签:cursor ListNode val list next 链表 移除 83 curr

c++

第一个方法

#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>

// Definition for singly-linked list.
struct ListNode {
    int       val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode* next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        // 头结点为 null, 返回 null
        if (nullptr == head) {
            return nullptr;
        }

        // 找到第一个不等于给定值的节点
        auto cursor = head;
        while (val == cursor->val) {
            cursor = cursor->next;
            if (nullptr == cursor) {
                break;
            }
        }
        // 如果没找到, 说明所有节点的值都等于给定值, 直接返回 null
        if (nullptr == cursor) {
            return nullptr;
        }

        // 创建一个新节点, 值为第一个不等于给定值的节点的值, 尾指针指向下一个节点
        ListNode* ret = new ListNode();
        ret->val      = cursor->val;
        ret->next     = cursor->next;

        // 循环剩余的节点
        cursor = ret;
        while (nullptr != cursor->next) {
            // 相等的节点删除
            if (val == cursor->next->val) {
                ListNode* tmp = cursor->next;
                cursor->next = tmp->next;
                tmp->next     = nullptr;
                delete tmp;
                continue;
            }

            // 不相等的跳过
            cursor = cursor->next;
        }

        return ret;
    }
};

static void fill_list(ListNode* list, std::vector<int> data) {
    ListNode* curr = list;
    for (auto i = 0; i < data.size(); i++) {
        curr->val = data[i];
        if (i < data.size() - 1) {
            curr->next = new ListNode();
            curr       = curr->next;
        }
    }
}

static void print_list(ListNode* list) {
    if (nullptr == list) {
        return;
    }
    ListNode* curr = list;
    while (nullptr != curr) {
        std::cout << curr->val << " ";
        curr = curr->next;
    }
    std::cout << std::endl;
}

int main() {
    auto l1 = std::make_unique<ListNode>();
    fill_list(l1.get(), {1,2,3,4,5,6});
    print_list(l1.get());

    auto solution = std::make_unique<Solution>();
    auto result   = solution->removeElements(l1.get(), 6);
    print_list(result);

    std::cout << "Hello World!" << std::endl;
}

java

第一个方法

ListNode.java

package com.laolang.leetcode;

public class ListNode {

    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

Solutio.java

package com.laolang.leetcode;

import java.util.Objects;

public class Solution {

    public ListNode deleteDuplicates(ListNode head) {
        if (Objects.isNull(head)) {
            return head;
        }

        ListNode ret = head;
        ListNode curr = ret;
        ListNode cursor = head;
        while (Objects.nonNull(cursor)) {
            if (curr.val == cursor.val) {
                cursor = cursor.next;
                continue;
            }

            curr.next = cursor;
            cursor = cursor.next;
            curr = curr.next;
        }
        curr.next = null;

        return ret;
    }
}

CommonTest.java

package com.laolang.leetcode;

import java.util.List;
import java.util.Objects;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.collections.Lists;

public class CommonTest {

    private Solution solution;
    private ListNode list1;


    private void fillList(ListNode list, List<Integer> data){
        ListNode node = list;
        for (int i = 0; i < data.size(); i++) {
            node.val =  data.get(i);
            if( i < data.size() - 1){
                node.next = new ListNode();
                node = node.next;
            }
        }
    }

    @BeforeClass
    public void beforeClass(){
        solution = new Solution();
        list1 = new ListNode();
        fillList(list1, Lists.newArrayList(1,1,2,3,3));
    }

    private void printList(ListNode list){
        if(Objects.isNull(list) ){
            return ;
        }
        ListNode curr = list;
        while( Objects.nonNull(curr)){
            System.out.print(curr.val);
            System.out.print(" ");
            curr = curr.next;
        }
        System.out.println();
    }

    @Test
    public void testOne(){
        printList(list1);

        ListNode ret = solution.deleteDuplicates(list1);
        printList(ret);
    }
}

标签:cursor,ListNode,val,list,next,链表,移除,83,curr
From: https://www.cnblogs.com/khlbat/p/17519105.html

相关文章

  • LeetCode 142. 环形链表 II
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode(intx):val(x),next(NULL){}*};*/classSolution{public:ListNode*detectCycle(ListNode*head){if(!head)return......
  • 基于DSP28335的三电平有源电力滤波器完整的软硬件资料
    完整的软硬件资料,其中包括两套基于DSP28335的三电平有源电力滤波器。这些资料可以直接使用。提取的知识点和领域范围:-三电平有源电力滤波器-DSP28335芯片原创文章,转载请说明出处,资料来源:http://imgcs.cn/5c/690782316603.html延申科普:三电平有源电力滤波器是一种用于电力系......
  • leetcode 19. 删除链表的倒数第 N 个结点
    链表问题,需要注意一下是倒着数还是正着数,和头结点会不会被删除即可publicListNoderemoveNthFromEnd(ListNodehead,intn){if(head==null){returnnull;}//头结点会被删除吗?intlength=0;ListNodep=......
  • 【leetcode】【83】【删除排序链表中的重复元素】
    c++第一个方法代码#include<algorithm>#include<iostream>#include<memory>#include<vector>//Definitionforsingly-linkedlist.structListNode{intval;ListNode*next;ListNode():val(0),next(nullptr){}......
  • 数据结构与算法 - 链表
    双链表的的基本结构从STL源码抽出的基本双链表结构代码#ifndef_GRAVER_GLIB_LIST_H_#define_GRAVER_GLIB_LIST_H_#include<cstddef>#include"graver/util/log_util.h"namespacegraver{//内部结构与方法放在detailnamespacedetail{/***@brief链表结......
  • 带头结点单链表插入,删除,查找与排序实现一个简单的基于链表结构的学生管理系统
    链表结构和操作方法////CreatedbyAdministratoron2023/6/12.//#ifndefCODE_LINKEDLIST_H#defineCODE_LINKEDLIST_H#include<iostream>#include<cstring>#include<stdlib.h>#include"student.h"typedefstructlink_list{//......
  • JavaScript 链表的增删改查
       //节点对象classNode{constructor(data){this.data=data;//存储节点数据this.next=null;//存储下一个节点的引用,默认为null}}//链表对象classLinkedList{constructor(){this.head=null;//链表头节点,默认为null}......
  • esp32运行边界路由与nRF52833作为rcp通过串口通讯组建边界路由步骤
    1、首先搭建esp32开发环境,按照官方指导说明,我这里是建立在linux运行环境下的  Linux和macOS平台工具链的标准设置-ESP32-C6-—ESP-IDF编程指南latest文档(espressif.com)   espressif/esp-idfatv5.2-dev(github.com)   去官网下载最新版的esp-id......
  • 143. 重排链表
    给定一个单链表L的头节点head,单链表L表示为:L0→L1→…→Ln-1→Ln请将其重新排列后变为:L0→Ln→L1→Ln-1→L2→Ln-2→…不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。示例1:输入:head=[1,2,3,4]输出:[1,4,2,3]>代码1(前......
  • Shuffle Cards (牛客多校) (rope 块状链表 用作可持续优化平衡树, 用于区间的整体移动
    rope:#include<ext/rope>usingnamespace__gnu_cxx; 定义方法:rope<变量类型>变量名称;人话解释:超级string算法解释:块状链表(即讲链表与数组的优势结合,形成分块思想)用途解释:这本来是一个用于快速操作string的工具,却一般被定义成int,然后用作可持久化线段树!insert(intpos,s......