Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given1->1->2
, return1->2
.
Given1->1->2->3->3
, return1->2->3
.
遍历一遍Linked List,将相同的元素删除,即可。public ListNode deleteDuplicates(ListNode head) { ListNode first = head; ListNode second; while(first != null){ second = first.next; if((second != null) && (second.val == first.val)){ first.next = second.next; }else{ first = second; } } return head; }
Remove Duplicates from Sorted List,布布扣,bubuko.com
Remove Duplicates from Sorted List
原文地址:http://blog.csdn.net/u010378705/article/details/29360459