一种怪异的节点删除方式

【题目】链表节点值类型为int型,给定一个链表中的节点node,但不给定整个链表的头结点。如何在链表中删除node?实现这个函数,并分析这么做会出现哪些问题。

【要求】时间复杂度O(1)

public class RemoveNode {
	public static class Node {
		public int value;
		public Node next;

		public Node(int value) {
			this.value = value;
		}
	}

	public static void removeNode(Node node) {
		if (node == null) {
			return;
		}
		Node next = node.next;
		if (next == null) {
			throw new RuntimeException("can not remove the last node");
		}
		node.value = next.value;
		node.next = next.next;
	}
}

猜你喜欢

转载自blog.csdn.net/gkq_tt/article/details/88378434