LeeCode-Remove Linked List Elements
2015-07-20 09:30
357 查看
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 NULL; if(head->next==NULL&&head->val==val) { return NULL; } if(head->next==NULL&&head->val!=val) { return head; } struct ListNode *p=head; while(p->val==val&&p->next!=NULL) { p=p->next; } if(p->val==val) { return NULL; } head=p; if(p->next==NULL) { return head; } while(p->next->next!=NULL) { if(p->next->val==val) { p->next=p->next->next; continue; } p=p->next; } if(p->next->val==val) { p->next=NULL; } return head; }
相关文章推荐
- C#枚举的简单使用
- LeetCode的一个题目 Divide Two Integers
- ListView的优化处理
- Python中文编码
- LeeCode-Insertion Sort List
- LeeCode-Pow(x, n)
- 【汇总】Android 常用方法整理
- 解决SSH会话连接超时问题
- 线程安全的SRILM语言模型c++接口
- Play!Framework 学习笔记(二):ActionInvoker源码分析
- linux 下的日志相关
- BeagleBone Black:透过SSH控制GPIO引脚
- LeeCode-Merge Sorted Array
- 【网络流】 HDU 3998 Sequence 最多不重合路径
- LeeCode-Power of Two
- List的总结
- JMX学习笔记(一)-MBean
- 【笔记】Service的使用
- 2015互联网女皇报告中文版
- cocos2dx APP图标文件更改(xcoder上传Archive ITMS-90032错误)