83.Remove Duplicates from Sorted List
题目大意:删除有序单链表中重复的数字,使得剩下的数字都唯一。例子如下:
法一:利用尾插,如果结点非重复则加入新链表结点。代码如下(耗时2ms):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public ListNode deleteDuplicates(ListNode head) { 2 if(head == null) { 3 return head; 4 } 5 ListNode res = head, cur = res; 6 head = head.next; 7 while(head != null) { 8 ListNode tmp = head; 9 //如果不重复,直接加入新的链表结点 10 if(cur.val != head.val) { 11 //尾插 12 cur.next = head; 13 cur = head; 14 } 15 head = tmp.next; 16 } 17 //由于是尾插,所以要将最后一个结点末尾置null 18 cur.next = null; 19 return res; 20 }