Java [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 list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
    	if(head == null)
    		return null;
        ListNode cur = head.next;
        ListNode pre = head;
        while(cur != null){
        	if(cur.val == pre.val){
        		if(cur.next != null){
        			cur.val = cur.next.val;
        			cur.next = cur.next.next;
        		}else{
        			pre.next = null;
        			cur = null;
        		}
        	} else {
        		pre = cur;
        		cur = cur.next;
        	}
        }
        return head;
    }
}

思路二,递归解法:

public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)return head;
        head.next = deleteDuplicates(head.next);
        return head.val == head.next.val ? head.next : head;
}

  

时间: 2024-08-28 09:47:48

Java [Leetcode 83]Remove Duplicates from Sorted List的相关文章

(Java) 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 很简单的链表问题,可以写成递归和迭代两种形式.具体思路: 第一步,寻找第一个节点值和当前表头所指的节

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. 题目大意: 去除有序链表内部相同元素,即相同

leetcode:83 Remove Duplicates from Sorted List-每日编程第十六题

Remove Duplicates from Sorted List Total Accepted: 89961 Total Submissions: 253975 Difficulty: Easy 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-&g

Java [leetcode 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 memory. For example, Given input arra

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. 题目解析: LinkedList果然是各种快慢指针的解题啊! 两个Node, 一个fast, 一个lower 如果lowe

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. 备份头指针,分3种情况判断即可 public ListNode deleteDuplicates(ListNode hea

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