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* mergeTwoLists(ListNode* head) {
if (nullptr == head) {
return head;
}
ListNode* ret = head;
ListNode* curr = ret;
ListNode* cursor = head->next;
while (nullptr != cursor) {
if (curr->val == cursor->val) {
cursor = cursor->next;
continue;
}
curr->next = cursor;
cursor = cursor->next;
curr = curr->next;
curr->next = nullptr;
}
curr->next = nullptr;
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, 1});
print_list(l1.get());
auto solution = std::make_unique<Solution>();
auto result = solution->mergeTwoLists(l1.get());
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);
}
}