/**
* beyond your self and trust your self.
*
* @Author: lbc
* @Date: 2023-02-22 13:06
* @email: 594599620@qq.com
* @Description: oh my god! niu bi...
*/
public class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
单链表翻转方法:
public static Node reverseNode(Node head) {
Node pre = null;
Node next = null;
while (head != null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
/**
* 递归反转
*
* @param node
* @return
*/
public static Node reverse(Node node) {
Node newnode = null;
if (null == node || null == node.next) {
return node;
} else {
Node temp = node.next;
newnode = reverse(node.next);
temp.next = node;
node.next = null;
}
print(newnode);
return newnode;
}
/**
* 遍历节点数据
*
* @param node
*/
public static void print(Node node) {
System.out.print("node遍历:" + node.getValue());
while (node.next != null) {
node = node.next;
System.out.print("-->" + node.getValue());
}
System.out.println("\n");
}