Home > Database > Mysql Tutorial > CCI 2.3 删除单向链表中的某个节点,假设只能访问该节点

CCI 2.3 删除单向链表中的某个节点,假设只能访问该节点

WBOY
Release: 2016-06-07 15:43:20
Original
1162 people have browsed it

实现一个算法,删除单向链表中间的某个节点,假设你只能访问该节点。 示例: 输入:单向链表 a-b-c-d-e中的节点c 结果:不返回任何数据,但该链表变为:a-b-d-e package test;public class RemoveNode {//因为只能访问当前节点,没有办法得到之前节点的引用

实现一个算法,删除单向链表中间的某个节点,假设你只能访问该节点。

示例:

输入:单向链表 a->b->c->d->e中的节点c

结果:不返回任何数据,但该链表变为:a->b->d->e

package test;

public class RemoveNode {
	//因为只能访问当前节点,没有办法得到之前节点的引用,所以只能采用交换值得方法,
	//把之后节点的值赋给当前节点,然后删除之后的节点
	public void removeNode(Node node){
		if(node == null)
			return;
		if(node.next != null){
			node.val = node.next.val;
			node.next = node.next.next;
		}else{
			node = null;
		}
	}
}
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template