![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0700a08b5be2cce77947c956c293c4f8.png)
代码演示:
package swordfingeroffer;
public class InterviewQuestion24 {
public static class ListNode {
int value;
ListNode next;
public ListNode(int value) {
this.value = value;
}
}
public ListNode reverseList(ListNode pHead) {
if (pHead == null) {
throw new RuntimeException("链表不能为空!");
}
ListNode reverseNode = null;
ListNode pNode = pHead;
ListNode preNode = null;
while (pNode != null) {
ListNode pNext = pNode.next;
if (pNext == null) {
reverseNode = pNode;
}
pNode.next = preNode;
preNode = pNode;
pNode = pNext;
}
return reverseNode;
}
}