【leetcode】【206】【反转链表】
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;
}
}
Solution.java
package com.laolang.leetcode;
import java.util.Objects;
public class Solution {
public ListNode reverseList(ListNode head) {
if (Objects.isNull(head)) {
return null;
}
ListNode ret = head;
ListNode cursor = head.next;
ret.next = null;
while (null != cursor) {
ListNode tmp = cursor;
cursor = cursor.next;
tmp.next = ret;
ret = tmp;
}
return ret;
}
}
CommonTest
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));
}
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.reverseList(list1);
printList(ret);
}
}
本文来自博客园,作者:laolang2016,转载请注明原文链接:https://www.cnblogs.com/khlbat/p/17519124.html