解题思路
如果head.next为null 或者 head.next.next为null 就直接返回
1.头节点为空,我们设置temp=head.next.next;
2.让头节点的next指向空:head.next.next=null;(将第一个节点与后面的节点断开)
3.当temp不为空时,进入循环,首先定义一个节点cur=temp.next(为了后续的遍历)
4.让temp指向头节点的下一个节点(temp.next=head.next)
5.让头节点指向temp(head.next=temp)
6.让temp=cur,继续下一次循环
思路详解(字丑请多担待!)
代码实现
public void reverseBySelf(SingleLinkedList singleLinkedList){
head=singleLinkedList.head;
if (head.next==null||head.next.next==null){
return;
}
HeroNode temp=head.next.next;
head.next.next=null;
while (temp!=null){
HeroNode cur=temp.next;
temp.next=head.next;
head.next=temp;
temp=cur;
}
}