首页 > 其他 > 详细

203. 移除链表元素

时间:2019-06-02 19:22:07      阅读:66      评论:0      收藏:0      [点我收藏+]

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

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5

解法:没什么好说的,直接删即可
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while(head!=null && head.val == val){
            head = head.next;
        }
        if(head == null){
            return head;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur!=null){
            if(cur.val == val){
                pre.next = cur.next;
                cur.next = null;
                cur = pre;
            }else{
                pre = pre.next;
            }
            cur = cur.next;
        }
        return head;
    }
}

 

203. 移除链表元素

原文:https://www.cnblogs.com/czsy/p/10963832.html

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