/**
* 3.单链表的翻转(腾讯)
* @param head 需要翻转列表的头结点
* @return 翻转后链表的头结点
*/
public static HeroNode turnList(HeroNode head) {
//首先new end 节点
//每读取一个新节点,就添加到第一个位置
//返回end
if (head.next == null) {
System.out.println("链表为空~");
return null;
}
HeroNode end = new HeroNode(0, "", "");//新节点
int numList = numList(head);//计算此链表的长度
HeroNode curH = head.next;//辅助节点,用于遍历
for (int i = 0; i < numList; i++) {
//保存新节点的值
HeroNode node = curH;
//1
curH = curH.next;
//2
node.next = end.next;
end.next = node;
}
return end;
}