单链表
定义
//单链表
public static class Node{
public int value;
public Node next;
public Node(int data){
value = data;
}
}
单链表反转
新增1、2、3三个节点
public static Node reverseLinkedList(Node head){
Node pre = null;
Node next = null;
while (head != null){
//保存下next节点
next = head.next;
//头节点指针指向pre
head.next = pre;
//pre指向head节点
pre = head;
//head指向下一个节点
head = next;
}
return pre;
}
双链表
定义
//双链表
public static class DoubleNode{
public int value;
public DoubleNode last;
public DoubleNode next;
public DoubleNode(int data){
value = data;
}
}
双向链表反转
新增1、2、3三个节点
public static DoubleNode reverseDoubleList(DoubleNode head){
DoubleNode pre = null;
DoubleNode next = null;
while (head != null){
//保存下next节点
next = head.next;
//头节点next指针指向pre
head.next = pre;
//头节点last指针指向next
head.last = next;
//pre指向head节点
pre = head;
//head指向下一个节点
head = next;
}
return pre;
}