Leetcode#82Remove Duplicates from Sorted List II

Remove Duplicates from Sorted List II

Total Accepted: 40273 Total Submissions: 160820My Submissions

Question Solution

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

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

Show Tags

分析,依次遍历链表中数据,使用两个“指针”一个标识可能重复的起始点,一个标识可能重复的终止点

public class Solution {

public ListNode deleteDuplicates(ListNode head) {

ListNode X=new ListNode(0);

X.next=head;

ListNode p=X;

ListNode q=X;

while(p.next!=null)

{

if(p.next.next!=null)

{

if(p.next.next.val==p.next.val)

p=p.next;

else

{

if(p!=q)

{

q.next=p.next.next;

p=q;

}

else

{

q=q.next;

p=p.next;

}

}

}

else

{

if(p!=X)

{

if(p.val==p.next.val)

{

q.next=null;

p=q;

}

else

{

q=q.next;

p=p.next;

}

}

else

break;

}

}

return X.next;

}

}

时间: 2024-10-18 17:46:17

Leetcode#82Remove Duplicates from Sorted List II的相关文章

[leetcode]Remove Duplicates from Sorted List II @ Python

原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4-&g

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

LeetCode: Remove Duplicates from Sorted List II [083]

[题目] Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2-

LeetCode: Remove Duplicates from Sorted Array II [080]

[题目] Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. [题意] 给定一个有序数组,给数组去重,和Remove Duplicates fro

leetcode——Remove Duplicates from Sorted List II 删除排序字符串中重复字符(AC)

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3

LeetCode——Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3

(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2

Leetcode: Remove Duplicates from Sorted List II 解题报告

Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->

Leetcode#80Remove Duplicates from Sorted Array II

Remove Duplicates from Sorted Array II Total Accepted: 39950 Total Submissions: 130101My Submissions Question Solution Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2