【数据结构】链表---移除链表元素

本文介绍如何删除链表中等于特定值的所有元素。例如,给定链表1->3->5->7,若要删除值为3的节点,处理后链表将变为1->5->7。在Python中,通过创建Node类,定义Solution构造方法,使用两个引用cur和prev遍历链表。当cur.val等于目标值时,prev的next指向cur的next,实现删除操作。对于头节点为目标值的情况,可以采用三种方法处理:直接使head指向下一个节点,避开头节点后再判断,或使用临时变量tmpHead记录头节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

删除链表中等于给定值 val 的所有节点
示例:
输入: 1->3->5->7, val = 3
输出: 1->5->7
代码:
建立一个 Node 类,val 代表目标值,next代表链表中结点的下一个指向

class Node {
	int val;	
	Node next;	// 如果 next == null 表示是最后一个结点

	Node(int val) {
		this.val = val;
		this.next = null;
	}
	
	public String toString() {
		return String.format("Node(%d)", val);
	}
}

写构造方法 Solution
定义两个引用 cur 和 prev,cur 进行遍历,cur 和 prev同时向后移动;
如果 cur.val == val ,即出现目标值,
则目标值的前驱 prev 将指向目标值所指向的对象,即删除目标值所在的结点

若出现链表第一个结点为目标值的情况,有三种解决方法
第一种:
cur 从第一个结点开始遍历,若第一个结点为目标值,则使 head 指向下一个结点对象即可

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode prev = null;
        ListNode cur = head;
        
        while (cur != null) {
            if (cur.val == val) {
                if (cur == head) {
                    head = cur.next;
                } else {
                    prev.next = cur.next;
                }
            } else {
                prev = cur;
            }
            
            cur = cur.next;
        }
        
        return head;
    }
}

第二种:
先避开第一个结点,直到 cur 遍历完整个链表,最后再去判断第一个结点是否为目标值,如果是,直接让 head 指向下一个结点对象·

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        
        while (cur != null) {
            if (cur.val == val) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            
            cur = cur.next;
        }
        
        if (head.val == val) {
            head = head.next;
        }
        
        return head;
    }
}

第三种:
定义一个临时变量为 tmpHead,
最后还是返回 head ,
直接避免了第一个结点对象为目标值的情况

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode tmpHead = new ListNode(-1);
        tmpHead.next = head;
        ListNode prev = tmpHead;
        ListNode cur = head;
        
        while (cur != null) {
            if (cur.val == val) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            
            cur = cur.next;
        }
        
        return tmpHead.next;
    }
}

写主函数,校验代码;

private static void print(Node head) {
		System.out.println("打印链表:");
		for (Node cur = head; cur != null; cur = cur.next) {
			System.out.print(cur + " --> ");
		}
		System.out.println("null");
	}
	private static Node createList2() {
        Node n1 = new Node(1);
        Node n2 = new Node(3);
        Node n3 = new Node(5);
        Node n4 = new Node(7);

        n1.next = n2;
        n2.next = n3;
        n3.next = n4;

        return n1;
    }
    public static void main(String[] args) {
		Node head = createList2();
		print(head);
		Node result = removeElements(head, 3);
		print(result);
	}
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值