用Java实现双向链表的倒置功能:
public class CustomLinkedList<E> {
Node<E> head;
Node<E> tail;
int size = 0;
/**
* 头加
* @param item
*/
public void add(E item) {
Node<E> node = new Node<>(item);
node.pre = null;
node.next = head;
if (head != null) {
head.pre = node;
} else {
tail = node;
}
size ++;
head = node;
}
private static class Node<E> {
E item;
Node<E> pre;
Node<E> next;
public Node (E item) {
this.item = item;
this.pre = null;
this.next = null;
}
}
/**
* 通过交换指针实现链表倒置(推荐)
*/
public void reverseBySwapPointer() {
Node<E> temp = null;
Node<E> current = head;
tail = head;
while (current != null) {
temp = current.pre;

本文详细介绍了如何使用Java编程语言实现双向链表的倒置操作,包括关键步骤和核心代码示例。
556

被折叠的 条评论
为什么被折叠?



