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

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

基本思路:
在这里提供两种方法
第一种:
遍历链表将值等于val的节点删去
第二种:
建立一个新的空链表,遍历原链表将每个与val值不同的对象节点尾插到新的链表中
具体过程看代码注释

代码:
在这里分开定义了两个类,注意两个定义类的java文档必须在同一个文件夹下

第一个Node类

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

第二个类:

public class Solution{
	//创建、初始化一个链表
	public static Node buildLinkedLiseHand(){
		//初始化对象
		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(6);
		Node n4 = new Node(3);
		Node n5 = new Node(4);
		Node n6 = new Node(5);
		Node n7 = new Node(6);
		//将节点连接起来
		n1.next = n2;
		n2.next = n3;
		n3.next = n4;
		n4.next = n5;
		n5.next = n6;
		n6.next = n7;
		return n1;
	}
	
	//遍历链表
	public static void printLinkedList(Node head){
		Node cur = head;
		while(cur != null){
			System.out.println(cur.val);
			cur = cur.next;
		}
	}
	
	//第一种方法
	//给定val删除与val值相同的节点
	//只要与val值相同的节点的前驱直接指向val值相同节点的下一个节点,就相当于删除了这个节点
	public static Node removeElements(Node head, int val){
		//用cur来遍历链表,让cur指向每个对象
		//prev是val的前驱节点,刚好比cur慢一步
		Node cur = head;
		Node prev = null;
		//循环结束的条件就是遍历完整个链表,即cur为null的时候结束
		while(cur != null){
			if(cur.val == val){            //判断如果此时cur所指向的值等于val,进行下边的操作
				if(cur == head){           
					head = head.next;      //如果此时cur指向与head相同说明链表中第一个数的值等于val,直接跳过这个值,相当于删除掉了这个值
				}else{
					prev.next = cur.next;  //在cur.val == val 的条件下让prev.next指向和cur.next相同的地方(此时prev比cur慢一步,刚好就可以跳过val值所在的节点)
				}
			}else{ 
			prev = cur;                    //如果此时的值不等于val,在这里让prev指向和此时cur指向相同的地方
			}
			cur = cur.next;                //cur指向下个地方,这样就可以刚好让prev比cur慢一步
		}
		return head;
	}
	
	/*
	//第一种方法(2)
	//先判断第一个数是不是等于val,如果等于直接让head = head.next,如果第一个数是空,直接返回null
	public static Node removeElements(Node head, int val){
		if(head == null){
			return null;
		}
		if(head.val == val){
			head = head.next;
		}
		Node cur = head;
		Node prev = null;
		while(cur != null){
			if(cur.val == val){
				prev.next = cur.next;
			}else{
				prev = cur;
			}
			cur = cur.next;
		}
		return head;
	}
	*/
	
	/*
	//第二种方法
	//建立一个新的空链表头节点为newHead,遍历原链表将每个与val值不同的对象节点尾插到新的链表中
	public static Node removeElements(Node head, int val){
		//newHead为新链表的头节点,last是新链表中指向最后一个对象的节点
		Node cur = head;
		Node last = null;
		Node newHead = null;
		//循环结束的条件就是遍历完整个链表,即cur为null的时候结束
		while(cur != null){
			if(cur.val == val){             //如果此时指向的对象的值的关于val,直接跳过这个对象,让cur指向下个对象
				cur = cur.next;
			}else{                          //如果值不等于val,则就要将指向这个对象的节点插入新链表中,执行下边的操作
				if(newHead == null){        //如果新的链表头节点指向为空说明此时新链表中还没有节点,直接插入要插入的节点
					newHead = cur;          //就是让新链表的头节点指向与插入的对象的节点相同
					last = cur;             //此时的last也就会变成新插入的节点
				}else{                      //如果新的链表头节点不为空
					last.next = cur;        //那么就进行尾插,插入节点(就是让上一次的last.next指向等于新插入的节点)
					last = cur;             //此时的last也就会再次变成新插入的节点
				}
				cur = cur.next;             //然后再让cur去指向下一个对象
			}
		}
		if(last != null){                   //如果结束之后的last不为空,说明最后一个数值等于val
			last.next = null;               //此时结束,即让last.next = null;
		}
		return newHead;
	}
	
	*/
	public static void main(String[] args){
		Node head = buildLinkedLiseHand();
		printLinkedList(head);
		System.out.println("删除链表中等于给定值6的所有节点后链表为:");
		head = removeElements(head, 6);
		printLinkedList(head);
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值