题目:
给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例:
输入: 1->1->2->3->3
输出: 1->2->3
代码:
public class Test27 {
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public ListNode deleteDuplicates(ListNode head) {
ListNode result = head;
while (head!=null && head.next != null) {
ListNode next = function(head);
head.next=next;
head = next;
}
return result;
}
public ListNode function(ListNode head){
if (head.next==null) {
return null;
}
if (head.next.val!=head.val) {
return head.next;
}
return function(head.next);
}
}