首页 > 其他 > 详细

leetcode 203. 移除链表元素

时间:2019-11-29 19:06:30      阅读:74      评论:0      收藏:0      [点我收藏+]

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5
 1 class Solution {
 2     public ListNode removeElements(ListNode head, int val) {
 3         if(head==null){
 4             return head;
 5         }
 6         ListNode iterator = head;
 7         ListNode pre = null;
 8         ListNode next = null;
 9         while(iterator!=null){
10             next = iterator.next;
11             if(iterator.val==val){
12                 if(pre==null){
13                     head=iterator.next;
14                 }else{
15                     pre.next=iterator.next;
16                 }
17             }else{
18                 pre=iterator;
19             }
20             iterator=next;
21         }
22         return head;
23     }
24 }

 

leetcode 203. 移除链表元素

原文:https://www.cnblogs.com/gongzixiaobaibcy/p/11959217.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!