Remove Duplicates from Sorted List ,除去链表中相邻的重复元素

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.

public ListNode deleteDuplicates(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}
		ListNode temp = head;
		while (temp.next != null) {
			if (temp.next.val == temp.val) {
				temp.next = temp.next.next;
			}
			else
			{
				temp = temp.next;
			}
		}
		return head;
	}

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.

算法分析:区别问题1,这个是把所有重复元素都删掉,一个不留,所有要用两重while去重,上面一题去重时只需要一重循环。

public ListNode deleteDuplicates2(ListNode head)
	{
		if(head == null || head.next == null)
		{
			return head;
		}
		ListNode pre = new ListNode(0);//记录头结点,因为开头如果有重复元素,head将改变
		pre.next = head;
		ListNode temp = pre;
		while (temp.next != null && temp.next.next != null) {
			if(temp.next.val == temp.next.next.val)
			{
				int dup = temp.next.val;
				while(temp.next != null && temp.next.val == dup)//去掉所有重复元素
				{
					temp.next = temp.next.next;
				}
			}
			else
			{
				temp = temp.next;
			}
		}
		return pre.next;
	}
时间: 2024-10-23 07:37:56

Remove Duplicates from Sorted List ,除去链表中相邻的重复元素的相关文章

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. 如题目所诉,去除递增链表中重复值的节点. 刚开始思路如下: 设置一个指针用于遍历全部节点 让每个节点和其next节点值比较

[LeetCode] 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. For example,Given input array A = [

[leetcode]83. Remove Duplicates from Sorted List有序链表去重

Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 题意: 有序链表去重 思路: 代码: 1 class Solution { 2 public Lis

[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 解法:设置两个指

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. 思路:两个指针,一个指向当前节点cur,一个指向下一个节点nx,终止条件,也就是比较了len-1次. 比较若不等,则两指针

[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 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 有序数组中去除重复项之二

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 from Sorted Array 有序数组中

[LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, 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