leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的重复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.

思路:这个题在刚开始做的时候想的有点,怎么都没办法正确解出。后面把代码全部删除重写,思路是记录当前节点p=head,然后head往下遍历,当head的值不等于head.next的值时,结束。比较p==head,相等说明没有重复,连接上;不相等说明有重复,跳过即可。

具体代码如下:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {

        ListNode first = new ListNode(0);
        ListNode last = first;

        ListNode p = head;

        while(head != null){
        	while(head.next != null){//p不动,head后移直到head.next与p不相等
        		if(p.val == head.next.val){
        			head = head.next;//相等循环
        		}else{
        			break;//不相等结束
        		}
        	}
        	if(p == head){//只有一个
        		last.next = p;//添加
        		last = last.next;
        	}
        	p = head = head.next;//有多个则不添加
        	last.next = null;//去掉关联
        }
		return first.next;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-30 14:20:11

leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的重复II) 解题思路和方法的相关文章

leetCode 83.Remove Duplicates from Sorted 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. 思路:此题与上一题异曲同工,具体解法如下: /** * Definition for singly-linked li

leetCode 83.Remove Duplicates from Sorted 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. 思路:此题与上一题异曲同工,详细解法例如以下: /** * Definition for singly-linked

力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现

题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2->3->3输出: 1->2->3 英文: Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->

LeetCode(82): 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 82.Remove Duplicates from Sorted List II (删除排序链表的反复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 82 Remove Duplicates from Sorted List 2

* Problem: * Given a sorted linked list, delete all nodes that have duplicate numbers * leaving only distinct numbers from the original list. * Solution: * Compare the current position with the previous and behind it. If it does not equal to them, th

[LeetCode]82. Remove Duplicates from Sorted 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. Subscribe to see which companies asked this question 解法:设置两个指

[leetcode]82. Remove Duplicates from Sorted List

第一题:遍历链表,遇到重复节点就连接到下一个. public ListNode deleteDuplicates(ListNode head) { if (head==null||head.next==null) return head; ListNode res = head; while (head.next!=null){ if (head.val==head.next.val) { if (head.next.next!=null) head.next = head.next.next;

[LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)

①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2. 你不需要考虑数组中超出新长度后面的元素.示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回新的长度 5, 并且原数组