# 第二章 链表问题

## 2.4 反转单向和双向链表

### 【实现】

``````public class ReverseSingleLinkedList {

private static class Node {
public int value;
public Node next;

public Node(int value) {
this.value = value;
}
}

}

if (arr != null && arr.length != 0) {
Node preNode = this.head = new Node(arr[0]);
for (int i = 1; i < arr.length; i++) {
preNode = preNode.next = new Node(arr[i]);
}
}
}

return;
}
Node preNode = null;
Node curNode = null;
}
// Node pre = null;
// while (post != null) {
//     cur.next = pre;
//     pre = cur;
//     cur = post;
//    post = post.next;
// }
// cur.next = pre;
}

}

``````
``````public class ReverseDoubleLinkedList {

private static class Node {
public int value;

public Node last;
public Node next;

public Node(int value) {
this.value = value;
}
}

}

if (arr != null && arr.length != 0) {
Node preNode = this.head = new Node(arr[0]);
for (int i = 1; i < arr.length; i++) {
preNode.next = new Node(arr[i]);
preNode.next.last = preNode;
preNode = preNode.next;
}
}
}

return;
}
Node preNode = null;
Node curNode = null;
}
// while (post != null) {
//     cur.next = cur.last;
//     cur.last = post;
//     cur = post;
//     post = post.next;
// }
}

}
``````
• Test.java
``````public class Test {

public Test(int[] arr) {
}

public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
Test test = new Test(arr);
}

}
``````