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

        ListNode first = new ListNode(0);
        ListNode last = first;

        ListNode p = head;

        while(head != null){
        	while(head.next != null){//去除重复项
        		if(p.val == head.next.val){
        			head = head.next;
        		}else{
        			break;
        		}
        	}
        	last.next = p;//每项只添加一个值
        	last = last.next;
        	p = head = head.next;
        	last.next = null;
        }
		return first.next;
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-12 23:59:51

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

[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->2Output: 1->2Example 2: Input: 1->1->2->3->3Output: 1->2->3 有序链表去重. 解析 移除给定有序链表的重复项,那么我们可以遍历这个链表,每个结点和其后面的结点比较

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

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

[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