remove duplicates from sorted list

Leetcode83题1.问题:
Given a sorted linked list, delete all duplicates such that each element appear only once.
2.思路:
该题和第26题解法一样,只不过26题是数组,本题是链表
3.实现:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) 
{
    struct ListNode *p, *q;
    if(head == NULL){
    return NULL;
    }
    p = q = head;
    while(p != NULL){
    if(p->val != q->val){
        q->next = p;
        q = q->next;
    }
    p = p->next;
    }
    q->next = NULL;
    return head;
}