一.实现思路
1)准备一个新的头节点
2)准备一个指针变量cur指向第一个节点
3)对链表进行遍历
4)先保存cur的下一个节点到next变量,要不节点会丢失 Node next = cur.next
5)cur.next = newHead.next
6) newHead.next = cur
7) 再让cur 指向 下一个节点next, cur = next
8)最后,将原来的head节点指向newHead的下一个节点 head.next = newHead.next
二.代码实现
//单链表的反转
public void reverseList(Node head){
if(head.next == null || head.next.next == null){
return;
}
Node newHead = new Node(-1, "");
Node cur = head.next;
while (cur != null) {
Node next = cur.next;
cur.next = newHead.next;
newHead.next = cur;
cur = next;
}
head.next = newHead.next;
}