[leetcode-203]Remove Linked List Elements(c)
2015-08-26 19:42
267 查看
问题描述:
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
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
分析:注意链表的头部即可。
代码如下:8ms
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
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
分析:注意链表的头部即可。
代码如下:8ms
[code]/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeElements(struct ListNode* head, int val) { if(!head) return head; struct ListNode* tmpNode=head; struct ListNode* prevNode = NULL; while(tmpNode){ if(tmpNode->val==val){ if(!prevNode){ head = tmpNode->next; }else{ prevNode->next = tmpNode->next; } }else{ prevNode = tmpNode; } tmpNode = tmpNode->next; } return head; }
相关文章推荐
- LeetCode题解:Excel Sheet Column Number
- poj 2299 Ultra-QuickSort(归并排序或是bit 树+离散化皆可)
- jdbc实例
- 华为oj: 图片整理
- Linux的PXE kickstart网络自动装机
- log 工具
- Unity中Instantiate物体失效问题
- PE文件学习(三)数据目录表之资源
- A+B Problem
- ExpanableListView
- 华为机试之好友管理
- Atlassian JIRA 插件开发(二) — 插件项目框架搭建
- 7-Neural Networks--Learning
- hdu 2066 一个人的旅行 (单源最短距离)
- LeetCode题解:Excel Sheet Column Title
- Cocos2dx学习笔记4:导演(Director)
- Proud Merchants
- SGU 224
- JavaScript控制各种浏览器全屏模式的方法、属性和事件介绍
- Proud Merchants