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.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head)
{
if(head==NULL)
return NULL; if(head!=NULL&&head->next==NULL)
return head; if(head->next->next==NULL)
{
if(head->val==head->next->val)
{
head->next==NULL;
return head->next;
} if(head->val!=head->next->val)
{
return head;
}
} struct ListNode* p;
p=head; int count=;
while(p!=NULL)
{
p=p->next;
count++;
} int *array;
array=(int *)malloc(count*sizeof(int)); p=head;
int i=;
while(p!=NULL)
{
array[i]=p->val;
i++;
p=p->next;
} p=head;
for(i=;i<count-;i++)
{
if(array[i]!=array[i+])
{
p->val= array[i];
p=p->next;
}
} p->val=array[count-];
p->next=NULL; return head;
}

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

  1. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  6. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  10. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

随机推荐

  1. seajs教程之seajs学习笔记 seajs.use用法

    seajs.use 用来在页面中加载模块.通过 use 方法,可以在页面中加载任意模块. 实例地址:http://www.android100.org/html/201405/23/12807.htm ...

  2. Spring集成Hessian

    一.概述 Spring 通过org.springframework.remoting.caucho.HessianServiceExporter将POJO中的所有public方法发布为Hessian服 ...

  3. Live555 分析(二):服务端

    live555支持单播和组播,我们先分析单播的流媒体服务端,后面分析组播的流媒体服务端. 一.单播的流媒体服务端: // Create the RTSP server: RTSPServer* rts ...

  4. handsontable插件事件

    Hook插件 afterChange (changes: Array, source: String):1个或多个单元格的值被改变后调用     changes:是一个2维数组包含row,prop,o ...

  5. UVA 673 (13.08.17)

     Parentheses Balance  You are given a string consisting of parentheses () and []. Astring of this ty ...

  6. Tomcat 常见问题篇

    Tomcat 常见问题一.Tomcat常见问题 1.Tomcat web容器出现故障时,我们通过Tomcat自带的logs查看原因,以下错误提示都是源于logs 2.Connection refuse ...

  7. (转载)js获取JqueryString方法小结

    一.<script>urlinfo=window.location.href; //获取当前页面的urllen=urlinfo.length;//获取url的长度offset=urlinf ...

  8. NO.14 两个div并排,左边为绝对宽度,右边为相对宽度

    两个div并排,左边为绝对宽度,右边为相对宽度,这个问题,我也经常遇到,我一般的处理方法是将最大的容器padding-left固定宽度,左边的固定宽度的一块position:absolute,然后ri ...

  9. JavaScript中String对象的match()、replace() 配合正则表达式使用

    正则表达式由来已久,查找替换功能非常强大,但模板难记复杂. JavaScript中String对象的match().replace()这2个方法都要使用正则表达式的模板.当模板内容与字符串不相匹配时, ...

  10. jxl读和取Excel文件

    请参看下面链接: jxl如何读和取excle中的数据