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-11-10 15:08:31

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

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

力扣—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, 并且原数组