-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMain.java
More file actions
99 lines (93 loc) · 2.44 KB
/
Main.java
File metadata and controls
99 lines (93 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @auther: WJoe
* @Description
* @Date : 16:35 2018/10/16
*/
import java.util.Scanner;
class ListNode {
int val;
ListNode next;
public ListNode(int x) {
this.val = x;
}
}
public class Main {
ListNode head = null;
public void add(int data) {
ListNode newNode = new ListNode(data);
//添加头结点
if (head == null) {
head = newNode;
return;
}
ListNode temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public void printNode(ListNode head) {
ListNode temp = head;
while (temp != null) {
System.out.print(temp.val + " ");
temp = temp.next;
}
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
ListNode list = null;
if (l1.val < l2.val) {
list = l1;
list.next = mergeTwoLists(l1.next, l2);
} else {
list = l2;
list.next = mergeTwoLists(l1, l2.next);
}
return list;
}
public static ListNode DeleteDupliacateNode1(ListNode head) {
ListNode pPre = head;
ListNode pCur = pPre.next;
ListNode pNext;
boolean bDup;
while (pCur != null) {
pNext = pCur.next;
bDup = false;
while (pNext != null && pCur.val == pNext.val) {
pPre.next = pNext;
pCur = pNext;
pNext = pCur.next;
bDup = true;
}
if (bDup)//pCur与原数据重复,删除
{
pPre.next = pNext;
} else {
pPre = pCur;//pCur没有重复,将pPre向后移动
}
pCur = pNext;
}
return head;
}
public static void main(String[] args) {
Scanner fin = new Scanner(System.in);
int n = fin.nextInt();
int m = fin.nextInt();
Main l1 = new Main();
for (int i = 0; i < n; i++) {
l1.add(fin.nextInt());
}
Main l2 = new Main();
for (int i = 0; i < m; i++) {
l2.add(fin.nextInt());
}
ListNode list = l1.mergeTwoLists(l1.head, l2.head);
Main merge = new Main();
merge.printNode(DeleteDupliacateNode1(list));
}
}