LeeCode-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  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 struct ListNode* deleteDuplicates(struct ListNode* head)
 9 {
10     if(head==NULL)
11         return NULL;
12
13     if(head!=NULL&&head->next==NULL)
14         return head;
15
16
17     if(head->next->next==NULL)
18     {
19         if(head->val==head->next->val)
20         {
21             head->next==NULL;
22             return head->next;
23         }
24
25         if(head->val!=head->next->val)
26         {
27             return head;
28         }
29     }
30
31
32      struct ListNode* p;
33      p=head;
34
35      int count=0;
36      while(p!=NULL)
37      {
38          p=p->next;
39          count++;
40      }
41
42
43      int *array;
44      array=(int *)malloc(count*sizeof(int));
45
46      p=head;
47      int i=0;
48      while(p!=NULL)
49      {
50          array[i]=p->val;
51          i++;
52          p=p->next;
53      }
54
55      p=head;
56      for(i=0;i<count-1;i++)
57      {
58         if(array[i]!=array[i+1])
59         {
60             p->val=    array[i];
61             p=p->next;
62         }
63      }
64
65      p->val=array[count-1];
66      p->next=NULL;
67
68      return head;
69 }
时间: 2024-10-25 14:58:50

LeeCode-Remove Duplicates from Sorted List的相关文章

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I 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 memor

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] 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 consta

26. Remove Duplicates from Sorted Array【leetcode】,数组,array,java,算法

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 mem

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. /** * Definition fo

LeetCode(82): Remove Duplicates from Sorted List II

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

LintCode - Remove Duplicates from Sorted List

LintCode - Remove Duplicates from Sorted List LintCode - Remove Duplicates from Sorted List Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list/ Description Given a sorted linked list, del

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刷题笔记】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]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

LeetCode:Remove Duplicates from Sorted Array &amp;&amp; Remove Element

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 =