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