问题:删除一个链表最后一个节点,只知道首节点first.
思路:因为只知道首节点,所以只能历遍链表,找到最后两个节点,将倒数第二个节点的next定义为null,无法关联最后一个节点,就可以实现。(之所以不直接让最后一个为空,是为了保证准确性)
代码:
package chapter1.a3
;
public class Example1319 {
private class Node{
int
item
;
Node
next
;
}
private Node
first
;
private void
deleteLastNode(){
Node current =
first
;
if (current ==
null)
return;
Node next = current.
next
;
//如果next为空,那么就只有一个first存在,所以直接first为空就可以了
if (next ==
null)
first =
null;
while (next.
next !=
null){
current = next
;
next = next.
next
;
}
current.
next =
null;
// 这里为什么不能够直接写 next = null;
// 因为next = null 并不意味着 current 访问不到 current.next,还是可以的,
// 所以要写为 current.next = null, 这样才能保证删除最后一个节点
}
}