1. 遍历反转
public NodeBean reverse1() {
NodeBean node = headNode;
NodeBean pre = null;
NodeBean next = null;
while (node != null) {
next = node.nextNode;
node.nextNode = pre;
pre = node;
node = next;
}
return pre;
}
2.递归反转
public NodeBean reverse2() {
return reverse2(headNode);
}
public NodeBean reverse2(NodeBean targetNode) {
if (targetNode == null || targetNode.nextNode == null) {
return targetNode;
}
NodeBean tempNode = targetNode.nextNode;
NodeBean newNode = reverse2(targetNode.nextNode);
tempNode.nextNode = targetNode;
targetNode.nextNode = null;
return newNode;
}