(leetcode)Remove Duplicates from Sorted List
2015-06-10 20:24
363 查看
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given
Given
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; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if(head== NULL || head->next == NULL) return head; ListNode *p = head; ListNode *q = NULL; int a,b; while(p->next) { q=p->next; a=p->val; b=q->val; if(a==b) { ListNode *tmp=q; q=q->next; p->next = q; free(tmp); } else { p = q; q = q->next; } } return head; } };
相关文章推荐
- Xcode6在iPhone5+iOS7模拟器上编译,上下有黑边问题
- System权限下进程遇到的问题以及如何降权启动进程
- 【VB.NET机房重构】SQLHelper--数据库小助手
- Majority Element
- 我们都爱大长腿
- Algorithms—22.Generate Parentheses
- XX项目总结
- uva 10397 Connect the Campus(最小生成树kruskal)
- 二进制bomb实验第三弹
- android:layout_gravity 和 android:gravity 的区别
- sprintf函数
- Canjs基础教程之Controls
- HDU - 2059 龟兔赛跑(多阶段决策dp)
- 线程与进程的区别
- oracle record is locked by another user
- DFS Path Sum II
- 仿英语流利说取词放大控件的实现
- Java——(四)Collection之Set集合TreeSet类
- 修改input的type属性
- 2.2linux内核移植简介