首页 > 其他 > 详细

203. Remove Linked List Elements - LeetCode

时间:2018-07-08 16:27:43      阅读:173      评论:0      收藏:0      [点我收藏+]

Question

203.?Remove Linked List Elements

技术分享图片

Solution

题目大意:从链表中删除给定的数

思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点

Java实现:

public ListNode removeElements(ListNode head, int val) {
    ListNode cur = head;

    while (cur != null) {
        if (cur.next != null && cur.next.val == val) {
            ListNode tmp = cur.next.next;
            cur.next = tmp;
        } else {
            cur = cur.next;
        }
    }
    return (head != null && head.val == val) ? head.next : head;
}

203. Remove Linked List Elements - LeetCode

原文:https://www.cnblogs.com/okokabcd/p/9280062.html

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