leetcode 203: Remove Linked List Elements
2015-08-26 12:26
459 查看
Create a dummy head before head. The rest is easy.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyhead=new ListNode(-1);
dummyhead->next=head;
ListNode *curr=dummyhead;
while(curr->next)
{
if(curr->next->val==val)
{
ListNode *temp=curr->next;
curr->next=temp->next;
delete temp;
}
else
curr=curr->next;
}
head=dummyhead->next;
delete dummyhead;
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode* dummyhead=new ListNode(-1);
dummyhead->next=head;
ListNode *curr=dummyhead;
while(curr->next)
{
if(curr->next->val==val)
{
ListNode *temp=curr->next;
curr->next=temp->next;
delete temp;
}
else
curr=curr->next;
}
head=dummyhead->next;
delete dummyhead;
return head;
}
};
相关文章推荐
- jQuery实现的简洁下拉菜单导航效果代码
- 暑期报修项目经验分享二(附原码)
- 最老程序员创业开发实训2---采用MVC架构的应用Splash页面实现
- 二进制文件与文本文件区别
- Gradle 修改 Maven 仓库地址
- Fishnet(暴力POJ 1408)
- maven 安装
- android使用微软雅黑字体
- Fishnet(暴力POJ 1408)
- objc_msgSend crash
- 判断当前版本(转载)
- 【剑指offer】七,二进制表示中1的个数
- bzoj1023 [SHOI2008]cactus仙人掌图
- 宏观CMS-->功能体系结构内容管理系统
- 配流02——基于LOGIT的STOCH配流法(改进的dial算法)
- Android(java)学习笔记196:Android中Menu的使用(静态和动态)
- poj3659 Cell Phone Network(最小支配集-树形dp)
- 简单的安卓程序以及各文件作用
- HTTP 请求报文 响应报文
- leetcode 202: Happy Number