LeetCode 83. Remove Duplicates from Sorted List Add to List

Given a sorted linked list, delete all duplicates such that each element appear only once. 
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3. 
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head!=null){
            ListNode p=new ListNode(0);
            ListNode q=new ListNode(0);
            p=head;
            q=head.next;
            int k=0;
            while(q!=null){
                if(p.val==q.val){
                    q=q.next;
                    continue;
                }
                else{
                    p.next=q;
                    p=q;
                    q=q.next;
                }
            }
            p.next=null;       
            }
        return head;
    }
}

你可能感兴趣的:(Algorithm,LeetCode)