题意:给一个按值排好序的链表,删除其中的重复结点。
初步分析:大概,遍历删除,就可以吧。。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode p = head;
ListNode pre = head;
while(p != null)
{
pre = p;
p = p.next;
while(p!=null && p.val == pre.val)
{
p = p.next;
}
pre.next = p;
}
return head;
}
}