
在单向链表中实现append(追加)操作,其目标是将一个链表(源链表)的所有元素连接到另一个链表(目标链表)的末尾。例如,如果目标链表为 [0, 1, 2],源链表为 ['a', 'b'],那么append操作后的结果应为 [0, 1, 2, 'a', 'b']。
初学者常犯的一个错误是尝试直接将目标链表的 head.next 指向源链表的 head。这种做法是错误的,因为它只会将源链表的头部连接到目标链表的第二个节点之后,而丢弃了目标链表剩余的部分。正确的做法是找到目标链表的最后一个节点,然后将这个节点的 next 指针指向源链表的头节点。
实现append方法需要遵循以下步骤:
下面是一个完整的Java单向链表实现,包含了append方法以及辅助的add和printList方法。
public class LinkedList {
private Node head; // 链表的头节点
// 内部类,表示链表中的一个节点
private static class Node {
int data; // 节点存储的数据
Node next; // 指向下一个节点的引用
public Node(int data) {
this.data = data;
this.next = null;
}
}
// 向链表末尾添加元素的方法
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = newNode;
}
}
// 打印链表所有元素的方法
public void printList() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
/**
* 将另一个链表追加到当前链表的末尾。
*
* @param otherList 要追加到当前链表末尾的链表。
*/
public void append(LinkedList otherList) {
// 1. 处理源链表为空的情况
if (otherList == null || otherList.head == null) {
// 如果otherList为空或otherList没有元素,则无需追加,直接返回
return;
}
// 2. 处理当前链表(this)为空的情况
if (this.head == null) {
// 如果当前链表为空,则直接将otherList作为当前链表
this.head = otherList.head;
return;
}
// 3. 遍历当前链表,找到最后一个节点
Node current = this.head;
while (current.next != null) {
current = current.next;
}
// 4. 将当前链表的最后一个节点的next指针指向otherList的头节点
current.next = otherList.head;
}
public static void main(String[] args) {
// 创建第一个链表
LinkedList list1 = new LinkedList();
list1.add(0);
list1.add(1);
list1.add(2);
System.out.print("List1: ");
list1.printList(); // 输出: List1: 0 -> 1 -> 2 -> null
// 创建第二个链表
LinkedList list2 = new LinkedList();
list2.add(10);
list2.add(11);
System.out.print("List2: ");
list2.printList(); // 输出: List2: 10 -> 11 -> null
// 将list2追加到list1的末尾
list1.append(list2);
System.out.print("List1 after appending List2: ");
list1.printList(); // 输出: List1 after appending List2: 0 -> 1 -> 2 -> 10 -> 11 -> null
// 测试当前链表为空的情况
LinkedList emptyList = new LinkedList();
LinkedList singleNodeList = new LinkedList();
singleNodeList.add(99);
System.out.print("EmptyList: ");
emptyList.printList();
System.out.print("SingleNodeList: ");
singleNodeList.printList();
emptyList.append(singleNodeList);
System.out.print("EmptyList after appending SingleNodeList: ");
emptyList.printList(); // 输出: EmptyList after appending SingleNodeList: 99 -> null
// 测试源链表为空的情况
LinkedList list3 = new LinkedList();
list3.add(100);
LinkedList emptyOtherList = new LinkedList();
System.out.print("List3: ");
list3.printList();
System.out.print("EmptyOtherList: ");
emptyOtherList.printList();
list3.append(emptyOtherList);
System.out.print("List3 after appending EmptyOtherList: ");
list3.printList(); // 输出: List3 after appending EmptyOtherList: 100 -> null
}
}正确实现单向链表的 append 方法,关键在于准确找到目标链表的尾节点,并将其 next 指针指向源链表的头节点。同时,充分考虑并妥善处理空链表的边界条件,是编写健壮代码的重要一环。通过本文提供的实现思路和代码示例,读者可以清晰地理解并掌握这一核心链表操作。
立即学习“Java免费学习笔记(深入)”;
以上就是Java单向链表append方法实现指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号