leetcode第83题-Remove Duplicates from Sorted List
来源:互联网 发布:python datetime函数 编辑:程序博客网 时间:2024/04/29 21:22
这道题与实现数组中的删除重复元素类似。我们来看一下具体的过程,首先要判断是否为空(在这个上面吃了大苦头),删除下一个相同的元素的时候要定义一个tmp元素,再free掉即可。
#include<stdio.h>#include<stdlib.h>struct ListNode{int val;ListNode *next;};ListNode *deleteDuplicates(ListNode *head) {if (head) { struct ListNode *p = head;while (p->next) {if (p->val != p->next->val) {p = p->next;}else {struct ListNode *tmp = p->next;p->next = p->next->next;free(tmp);}}}return head;}int main(){int n;while(scanf("%d",&n)!=EOF){ListNode *head=(ListNode *)malloc(sizeof(ListNode));ListNode *p=head;p->next=NULL;for(int i=0;i<n;i++){int tmp;scanf("%d",&tmp);ListNode *q=(ListNode *)malloc(sizeof(ListNode));q->val=tmp;p->next=q;p=q;p->next=NULL;}ListNode *afterHead=deleteDuplicates(head->next);while(afterHead){printf("%d ",afterHead->val);afterHead=afterHead->next;}printf("\n");}return 0;}
1 0
- leetcode第83题-Remove Duplicates from Sorted List
- leetcode 第83题 Remove Duplicates from Sorted List
- LeetCode(83)Remove Duplicates from Sorted List
- [leetcode 83]Remove Duplicates from Sorted List
- [leetcode]83 Remove Duplicates from Sorted List
- [leetcode 83]Remove Duplicates from Sorted List
- leetcode-83 Remove Duplicates from Sorted List
- leetcode || 83、Remove Duplicates from Sorted List
- LeetCode 83 Remove Duplicates from Sorted List
- LeetCode---(83) Remove Duplicates from Sorted List
- leetcode 83 Remove Duplicates from Sorted List
- LeetCode 83:Remove Duplicates from Sorted List
- leetcode[83]:Remove Duplicates from Sorted List
- Leetcode[83]-Remove Duplicates from Sorted List
- Leetcode #83 Remove Duplicates from Sorted List
- leetcode 83: Remove Duplicates from Sorted List
- leetcode: (83) Remove Duplicates from Sorted List
- [Leetcode]#83 Remove Duplicates from Sorted List
- [乐意黎原创] chrome 显示 "a plugin (shockwave flash) isn't responding" 并停止响应的解决
- Java中初始化对象的顺序,静态代码块的用法以及Static的用法详解
- [codility]NailingPlanks
- windows下的C++ socket服务器(2)
- 放出一个借贷筹资平台性网站源码
- leetcode第83题-Remove Duplicates from Sorted List
- Add Binary
- NYOJ1100【WAJUEJI which home strong!】BFS+优先队列
- 惊艳的 cygwin——Windows下的Linux命令行环境的配置和使用
- Pentaho Report Designer 教程(一)初识
- 开发工具站点(持续更新。。)
- 软件开发过程中的各种图表
- VS2010编写自定义宏注释
- hello csdn