leetcode83-Remove Duplicates from Sorted List
public class LinkNode {
int val;
LinkNode next;
public LinkNode(int data) {
this.val = data;
this.next = null;
}
}
public class LinkList {
LinkNode head;
public LinkList() {
this.head = null;
}
public LinkNode getHead() {
return this.head;
}
//添加元素
public void addNode(int data) {
LinkNode node = new LinkNode(data);
if (this.head == null) {
this.head = node;
} else {
LinkNode cur = this.head;
while(cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
//正序打印
public void print(LinkNode node) {
while(node != null) {
System.out.print(node.val);
System.out.print(" ");
node = node.next;
}
System.out.println();
}
public void deleteRepeate() {
LinkNode pNode = this.head;
while(pNode != null && pNode.next != null) {
if(pNode.val == pNode.next.val) {
pNode.next = pNode.next.next;
} else {
pNode = pNode.next;
}
}
print(this.head);
}
}
public class removeDuplicatesfromSortedList {
public static void main(String[] args) {
LinkList list = new LinkList();
list.addNode(1);
list.addNode(1);
list.addNode(2);
list.addNode(3);
list.addNode(3);
list.deleteRepeate();
}
}