For example,
Given1->1->2, return1->2.
Given1->1->2->3->3, return1->2->3.
代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) { if(head == null || head.next == null){ return head; } ListNode second = head; ListNode first = head; while(second != null){ while(second != null && second.val == first.val){ second = second.next; } first.next = second; first = first.next; } return head; }}