LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)

题目(中文)

给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

输入: 1->2->3->3->4->4->5
输出: 1->2->5

示例 2:

输入: 1->1->1->2->3
输出: 2->3

英文
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:

Input: 1->1->1->2->3
Output: 2->3

这道题一开始思路和83题是差不多的,不过直接在里面加条件的话会超时
先在外面动态分配一个NewNode,再定义一个p代替NewNode进行操作

C++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==NULL||head->next==NULL) return head;
        ListNode* NewNode=new ListNode(0);
        NewNode->next=head;
        ListNode* p=NewNode;
        while(p->next){
            ListNode *q = p->next;
            while (q->next!=NULL && q->next->val == q->val) q = q->next;
            if (q != p->next) p->next = q->next;
            else p = p->next;
        }
        return NewNode->next;
    }
};

C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* deleteDuplicates(struct ListNode* head){
        if(head==NULL||head->next==NULL) return head;
        struct ListNode* NewNode=malloc(sizeof(struct ListNode));
        NewNode->next=head;
        struct ListNode* p=NewNode;
        while(p->next){//此时head
            struct ListNode *q = p->next;
            while (q->next!=NULL && q->next->val == q->val) q = q->next;
            if (q != p->next) p->next = q->next;
            else p = p->next;
        }
        return NewNode->next;
}

python3
# Definition for singly-linked list.
# class ListNode:
# def init(self, x):
# self.val = x
# self.next = None

class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if head==None or head.next==None:
            return head;
        NewNode=ListNode(0);
        NewNode.next=head;
        p=NewNode;
        while p.next:
            q = p.next;
            while q.next!=None and q.next.val == q.val: 
                q = q.next;
            if q != p.next: 
                p.next = q.next;
            else:
                p = p.next;
        return NewNode.next;

javascript

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var deleteDuplicates = function(head) {
   	 if(!head||!head.next) 
        return head;
        NewNode=new ListNode(0);
        NewNode.next=head;
        p=NewNode;
        while(p.next){
            q = p.next;
            while (q.next && q.val== q.next.val) q = q.next;
            if (q != p.next) p.next = q.next;
            else p = p.next;
        }
        return NewNode.next;
};
上一篇:博客园代码自定义高亮—测试


下一篇:栈的实现