您的位置:首页 > 其它

删除排序链表中的重复元素

2018-01-24 13:09 309 查看
/**

 * Definition of ListNode

 * class ListNode {

 * public:

 *     int val;

 *     ListNode *next;

 *     ListNode(int val) {

 *         this->val = val;

 *         this->next = NULL;

 *     }

 * }

 */

class Solution {

public:

    /*

     * @param head: head is the head of the linked list

     * @return: head of linked list

     */

    ListNode * deleteDuplicates(ListNode * head) {

        // write your code here

        if(head==NULL){

            return head;

        }

        ListNode *p,*q,*h,*h1;

        p=head;

        q=p;

        h=p;

        while(1){

            while(1){

                h1=h;

                h=h->next;

                if(h==NULL){

                    break;

                }

                q=h;

                if(p->val==q->val&&p!=q){

                    h1->next=h->next;

                    h=h1;

                }

                

            }

            p=p->next;

            h=p;

            if(p==NULL){

                break;

            }

        }

        return head;

    }

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: