203. Remove Linked List Elements - LeetCode

Posted okokabcd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了203. Remove Linked List Elements - LeetCode相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

203. Remove Linked List Elementseasy

#Leetcode# 203. Remove Linked List Elements

203. Remove Linked List Elements

203. Remove Linked List Elements

203. Remove Linked List Elements [easy] (Python)

[LeetCode]203. Remove Linked List Elements