Remove Linked List Elements

Source

Problem
Remove all elements from a linked list of integers that have value val.

Example
Given 1->2->3->3->4->5->3, val = 3, you should return the list as 1->2->4->5

题解

删除链表中指定值,找到其前一个节点即可,将 next 指向下一个节点即可。

Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    /**
     * @param head a ListNode
     * @param val an integer
     * @return a ListNode
     */
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode curr = dummy;
        while (curr.next != null) {
            if (curr.next.val == val) {
                curr.next = curr.next.next;
continue; }
curr = curr.next; }
return dummy.next; } }

源码分析

while 循环中使用 curr.next 较为方便,if 语句中比较时也使用 curr.next.val 也比较简洁,如果使用 curr 会比较难处理。

复杂度分析

 
posted @ 2023-07-22 14:07  凌雨尘  阅读(8)  评论(0编辑  收藏  举报