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->2->3->3, return 1->2->3.

思路:

维持两具指针指向当前节点与相邻的下一个节点。将当前节点与下一个节点值进行比较,相等,则删除下一个节点,不等则将两个指针同时递增1步。

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

leetcode:83 Remove Duplicates from Sorted List-每日编程第十六题的相关文章

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 (快慢指针)

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

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

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

(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 ☆(从有序数组中删除重复项)

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