Leetcode 单链表删除Remove Linked List Elements
2015-08-02 14:27
387 查看
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeElements(struct ListNode* head, int val) { if(head==NULL)return head; struct ListNode *p=NULL; while(head&&val==head->val){p=head;head=head->next;free(p);}//头 p=head; while(p&&p->next){; if(val==p->next->val){struct ListNode *tepm=p->next;p->next=tepm->next;free(tepm);} else p=p->next;}//内部或尾部 return head; }
相关文章推荐
- hdu2962Trucking 二分+最短路
- 蓝牙 相关知识
- Auth权限控制的实例代码演示(thinkphp)
- 请求转发和重定向的对比
- sql-99标准连接的语法
- 新开发模式下自动化测试
- sqlServer存储过程的加密和解密方法
- 北大ACM3734——Blocks
- UVA1610 PartyGame 聚会游戏(细节题)
- 设计单片机硬件系统应遵循的原则
- 模2运算的原理
- 在内核下使用事件对象KeInitializeEvent
- yum 命令跳过特定(指定)软件包升级方法
- android 键值(input)
- MainWindow简介
- android studio使用百度SDK开发百度地图应用中遇到的问题汇总
- OAuth2.0 原理
- mfc窗体坐标相关函数
- JavaScript
- 传值与传引用的 区别