首页 > 其他分享 >【leetcode】【1474】【删除链表 M 个节点之后的 N 个节点】

【leetcode】【1474】【删除链表 M 个节点之后的 N 个节点】

时间:2023-07-02 20:34:18浏览次数:49  
标签:ListNode val int list next 链表 1474 save 节点

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) {}
};

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;
}

#define PRINT_LOG

class Solution {
public:
    ListNode* deleteNodes(ListNode* head, int m, int n) {
        if (nullptr == head) {
            return head;
        }
        auto cursor = head;
        while (nullptr != cursor) {
            int  i    = 0;
            int  j    = 0;
            auto save = cursor;
            while (nullptr != save->next && i < m - 1) {
                save = save->next;
                i++;
            }
            if (nullptr == save->next) {
                break;
            }
            auto del = save->next;
            while (nullptr != del && j < n) {
                auto tmp   = del;
                del        = del->next;
                save->next = del;

                tmp->next = nullptr;
                delete tmp;
                tmp = nullptr;

                j++;
            }
            cursor = save->next;
        }
        return head;
    }
};

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;
        }
    }
}

void test_001() {
    std::cout << "==== test 001:" << std::endl;
    auto l1 = std::make_unique<ListNode>();
    fill_list(l1.get(), {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13});
    print_list(l1.get());

    auto solution = std::make_unique<Solution>();
    solution->deleteNodes(l1.get(), 2, 3);
    print_list(l1.get());
}

void test_002() {
    std::cout << "==== test 002:" << std::endl;
    auto l1 = std::make_unique<ListNode>();
    fill_list(l1.get(), {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11});
    print_list(l1.get());

    auto solution = std::make_unique<Solution>();
    solution->deleteNodes(l1.get(), 1, 3);
    print_list(l1.get());
}

int main() {
    system("chcp 65001");
    test_001();
    test_002();
    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;
    }
}

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, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13));
    }

    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.deleteNodes(list1, 2, 3);
        printList(ret);
    }
}

第一个方法

package com.laolang.leetcode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
public class Solution {

    public ListNode deleteNodes(ListNode head, int m, int n) {
        if (null == head) {
            return null;
        }
        ListNode cursor = head;
        while (null != cursor) {
            int i = 0;
            int j = 0;
            ListNode save = cursor;
            while (null != save.next && i < m - 1) {
                save = save.next;
                i++;
            }
            if (null == save.next) {
                break;
            }
            ListNode del = save.next;
            while (null != del && j < n) {
                del = del.next;

                save.next = del;

                j++;
            }
            cursor = save.next;
        }

        return head;
    }
}

标签:ListNode,val,int,list,next,链表,1474,save,节点
From: https://www.cnblogs.com/khlbat/p/17521334.html

相关文章

  • 【leetcode】【876】【链表的中间结点】
    c++第一个方法#include<algorithm>#include<iostream>#include<memory>#include<vector>//Definitionforsingly-linkedlist.structListNode{intval;ListNode*next;ListNode():val(0),next(nullptr){}Li......
  • Redis数据结构——链表
    前言Redis链表为双向无环链表!Redis使用了简单动态字符串,链表、字典(散列表)、跳跃表、整数集合、压缩列表这些数据结构来操作内存。本文继续来分析链表。 链表是一种非常常见的数据结构,在Redis中使用非常广泛,列表对象的底层实现之一就是链表。其它如慢查询,发布订阅,监视器等功......
  • 328. 奇偶链表
    难度中等707给定单链表的头节点 head ,将所有索引为奇数的节点和索引为偶数的节点分别组合在一起,然后返回重新排序的列表。第一个节点的索引被认为是 奇数 , 第二个节点的索引为 偶数 ,以此类推。请注意,偶数组和奇数组内部的相对顺序应该与输入时保持一致。你必须在......
  • 链表
    题目:小A打字时有不看屏幕的习惯。在一次小A打字时,调皮的小B常常趁小A不注意按下Home键和End键。当Home​键被按下时,输入光标会跳到文本最开头;当End键被按下时,输入光标会跳到文本末尾。现给出若干行按键的字符串,其中'['表示Home键,']'表示End键,其余字符均表示输入的内容,每行字符串......
  • LeetCoe-25-K个一组翻转链表
    25题:K个一组翻转链表题目给你链表的头节点head,每 k 个节点一组进行翻转,请你返回修改后的链表。k是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换......
  • 算法学习day03链表part01-203、707、206
    packageSecondBrush.LinkedList.LL1;/***203.移除链表元素*删除链表中等于给定值val的所有节点。*自己再次概述一下这个过程:*1.移除元素,要采用设置虚拟节点的方式,因为那样不需要考虑头结点问题*2.设置两个虚拟指向*3.移除元素就是遍历链表,然后碰到目标值......
  • 算法学习day04链表part02-24、19、0207、142
    packageSecondBrush.LinkedList.LL1;/***24.两两交换链表中的节点**/publicclassSwapNodesInPairs_24{publicListNodeswapPairs(ListNodehead){ListNodedummyhead=newListNode(-1);dummyhead.next=head;ListNodecur......
  • 【leetcode】【234】【回文链表】
    c++第一个方法#include<algorithm>#include<iostream>#include<memory>#include<vector>//Definitionforsingly-linkedlist.structListNode{intval;ListNode*next;ListNode():val(0),next(nullptr){}Li......
  • [刷题记录Day3]Leetcode链表专题
    #ListNodedefinitionpublicclassListNode{//结点的值intval;//下一个结点ListNodenext;//节点的构造函数(无参)publicListNode(){}//节点的构造函数(有一个参数)publicListNode(intval){this.val=val;......
  • 【leetcode】【206】【反转链表】
    c++第一个方法#include<algorithm>#include<iostream>#include<memory>#include<vector>//Definitionforsingly-linkedlist.structListNode{intval;ListNode*next;ListNode():val(0),next(nullptr){}Li......