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

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode deleteDuplicates(ListNode head) {
11         if(head==null||head.next==null)
12         return head;
13
14       ListNode heads=head;
15         ListNode p=head.next;
16         while(p!=null)
17         {
18             if(p.val==heads.val)
19             {
20                 p=p.next;
21                 heads.next=p;
22             }
23             else
24             heads=heads.next;
25         }
26         return head;
27     }
28 }
时间: 2024-11-03 22:42:17

83. Remove Duplicates from Sorted List的相关文章

<LeetCode OJ> 83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: Easy 题目意思:如今有一个已经排好顺序的链表,删除全部反复的节点.使每一个节点都仅仅出现一次! Given a sorted linked list, delete all duplicates such that each element appear only once. For exampl

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

【一天一道LeetCode】#83. Remove Duplicates from Sorted List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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-&

【LeetCode】83 - 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 nums 

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. 题目分析:简单来说就是去除有序链

【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. Solution: 1 /** 2 * Definition for singly-linked list. 3 * st

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

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