Remove Duplicates from Sorted List leetcode java

题目:

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.

题解:

这道题是经典的双指针问题,用两个指针一前一后指向链表。如果两个指针指向的值相等,那么就让第二个指针一直往后挪,挪到与第一个指针不同为止。然后让第一个指针的next指向第二个指针,两个指针同时往后挪,进行下面的操作。

需要注意,当list的结尾几个node是重复的时候,例如1->2->3->3,那么ptr2会指向null,需要特殊处理,令ptr1.next = null,这样list尾部就不会丢。

其他情况就不用特殊处理结尾了,因为结尾没有重复值,只须遍历就够了,不用特殊处理尾部。

代码如下:

1     public ListNode deleteDuplicates(ListNode head) {
 2         if(head == null || head.next == null)
 3             return head;
 4         
 5         ListNode ptr1 = head;
 6         ListNode ptr2 = head.next;
 7         
 8         while(ptr2!=null){
 9             if(ptr1.val == ptr2.val){
10                 ptr2 = ptr2.next;
11                 if(ptr2==null)
12                     ptr1.next = null;
13             }else{
14                 ptr1.next = ptr2;
15                 ptr1 = ptr1.next;
16                 ptr2 = ptr2.next;
17             }
18         }
19 
20         return head;
21     }

Remove Duplicates from Sorted List leetcode java

时间: 2024-10-25 12:05:36

Remove Duplicates from Sorted List leetcode java的相关文章

Remove Duplicates From Sorted Array leetcode java

算法描述: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array

leetcode 80 Remove Duplicates from Sorted Array II ----- java

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't

LeetCode – Remove Duplicates from Sorted List III (Java)

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 区别于上一题,这道不仅有index距离的限制,还加上了两值和的要求.这就要求

【Leetcode】Remove Duplicates from Sorted List in JAVA

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. 思路很简单,由于乖乖的sort好了,就是判断下一个是不是比它大就好了,如果大,那么跳过下一个直接link到下一个的下一

LeetCode – Remove Duplicates from Sorted List II (Java)

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.

Remove Duplicates from Sorted Array -- leetcode

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A = 

Remove Duplicates from Sorted List leetcode

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. 题目意思为删除链表中重复的元素 思路: 遍历链表,用两个指针,一个指向前一个,一个指向后一个,当两个所指向的值不相等时

83. Remove Duplicates from Sorted List Leetcode Python

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. 定义一个pre 和cur 1.当二者不等的时候pre.next=cur pre=pre.next 2.当相等的时候pr

leetCode-数组:Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素 考察数组的基本操作: class Solution { public int removeDuplicates(int[] nums) { if (nums==null || nums.length==0) return 0; int index = 1; for(int i =1; i<nums.length; i++){ if(nums[i]!=nums[i-1]){ nums[index]