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.

思路:

simple,从头到尾遍历结点,如果下一个结点不为空且当前结点和下一个节点相同,删除下一个结点,否则,遍历下一个结点。

代码:

public ListNode deleteDuplicates(ListNode head) {
		if(head==null)
			return head;
		ListNode p=head,q;
		while (p.next!=null)
		{
			q=p;
			if(q.val==p.next.val)
			{
				while(p.next!=null&&q.val==p.next.val)
					p=p.next;
				q.next=p.next;
				p=q.next;
				if(p==null)
					break;
			}
			else
				p=p.next;
		}
		return head;
    }

结果:

时间: 2024-08-12 14:14:46

leetcode_83_Remove Duplicates from Sorted List的相关文章

Remove Duplicates from Sorted Array II

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'

Leetcode | Remove Duplicates from Sorted Array I && II

Remove Duplicates from Sorted Array I 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 memor

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] 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 consta

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

26. Remove Duplicates from Sorted Array 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 mem

Leetcode-83 Remove Duplicates from Sorted List

#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 fo

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->

LintCode - Remove Duplicates from Sorted List

LintCode - Remove Duplicates from Sorted List LintCode - Remove Duplicates from Sorted List Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list/ Description Given a sorted linked list, del

leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法

Remove Duplicates from Sorted Array 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.

LeetCode_Remove Duplicates from Sorted List

一.题目 Remove Duplicates from Sorted List Total Accepted: 53757 Total Submissions: 157010My Submissions 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->