方法一(删除头结点时另做考虑)

class Solution {
public:
ListNode* removeElements(ListNode* head, int val) { if(head!=NULL && head->val==val)
{
head=head->next;
}
if(head==NULL) return NULL;
//处理第一位是val的情况
while(head->val==val)
{
head=head->next;
if(head==NULL) return NULL;
}
ListNode *curr=head->next;
ListNode *pre=head;
while(curr!=NULL)
{ if(curr->val==val)
{
pre->next=curr->next;
}
else
pre=curr;
curr=curr->next;
}
return head; }
};

方法二(添加一个虚拟头结点)

class Solution {
public:
ListNode* removeElements(ListNode* head, int val) { ListNode* vir=new ListNode(-);
vir->next=head;
ListNode *pre=vir;
while(pre->next!=NULL)
{ if(pre->next->val==val)
{
pre->next=pre->next->next;
}
else
pre=pre->next;
}
return vir->next; }
};

方法三(递归)

class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null)
return null;
head.next=removeElements(head.next,val);
if(head.val==val){
return head.next;
}else{
return head;
}
}
}

最新文章

  1. iOS-即时通讯-环信
  2. 显示Class 'Think\Controller\FuController' not found和Call to a member function assign() on a non-object 的错误问题
  3. mysql查看表使用的数据库引擎
  4. Golang与MySQL
  5. Stanford CoreNLP--功能列表
  6. fastjson将bean转成字符串时首字母变小写问题
  7. Jboss基础及简单的应用
  8. python 错误AttributeError: 'module' object has no attribute 'AF_INET'
  9. asp.net 二级域名session共享
  10. VS2015 'utf-8' codec can't decode byte
  11. Tensorflow 报错:tensorflow.python.framework.errors_impl.InternalError: Failed to create session.
  12. 对接https数据(3des加密)
  13. orchestrator HTTP接口forget-cluster误下线集群问题
  14. -webkit-,-moz-,-ms-,-o-具体指什么了?
  15. [BigData - Hadoop - YARN] YARN:下一代 Hadoop 计算平台
  16. POJ 3621Sightseeing Cows 0/1 分数规划
  17. linux系统部署Java程序获取ip时报Caused by: java.net.UnknownHostException: XXXXXXXXXX: XXXXXXXXXX: Name or service not known
  18. java编程中Properties类的具体作用和使用!
  19. django系列5.4--ORM中执行原生SQL语句, Python脚本中调用django环境
  20. HBase TableExistsException: hbase:namespace

热门文章

  1. bzoj2093 Frog
  2. Unity 2018 Cookbook (Matt Smith 著)
  3. vue项目搭建介绍01
  4. Paper | Noise2Noise: Learning Image Restoration without Clean Data
  5. java虚拟机规范学习笔记之数据类型
  6. 一、SqlServer查询今天的数据-多写法对比性能问题
  7. LeetCode 733: 图像渲染 flood-fill
  8. 模型的细致程度--Level of Development
  9. 【shell脚本】自动监控tomcat服务===autoCheck.sh
  10. 微软开放了.NET 4.5.1的源代码【转】