public int length(HeroNode head){
int length = 0;
if (null==head.next){
return 0;
}
HeroNode curHero = head.next;
while (null != curHero){
length++;
curHero = curHero.next;
}
return length;
}
public HeroNode findLastIndex(HeroNode head , int index){
if (null == head.next){
return null;
}
int size = length(head);
if (index>size || index<0){
System.out.println("index 超出范围");
return null;
}
HeroNode curHero = head.next;
for (int i = 0; i < size-index ; i++) {
curHero = curHero.next;
}
return curHero;
}
public void reverseList(HeroNode head){
if (null == head.next || null == head.next.next){
return;
}
HeroNode curHero = head.next;
HeroNode next = null;
HeroNode tempHead = new HeroNode(0,"","");
while (null != curHero){
next = curHero.next;
curHero.next = tempHead.next;
tempHead.next = curHero;
curHero = next;
}
head.next = tempHead.next;
}
public void reversePrint(HeroNode head){
if (null == head.next){
return;
}
Stack<HeroNode> stack = new Stack<>();
HeroNode curHero = head.next;
while (null != curHero){
stack.push(curHero);
curHero = curHero.next;
}
while (stack.size()>0){
System.out.println(stack.pop());
}
}
合并两个有序链表(待修改)
public HeroNode mergeSortedList(HeroNode head1 , HeroNode head2 ){
HeroNode head = new HeroNode(0,"","");
if (head1.next == null){
return head2;
}
if (head2.next == null){
return head1;
}
if (head1.next == null && head2.next == null){
return null;
}
HeroNode curFirst = head1.next;
HeroNode curSeond = head2.next;
HeroNode temp = head;
while (curFirst != null && curSeond != null){
temp = temp.next;
if (curFirst.no <= curSeond.no){
temp=curFirst;
curFirst = curFirst.next;
}else {
temp = curSeond;
curSeond = curSeond.next;
}
System.out.println(temp);
}
if (curFirst == null){
temp.next = curSeond;
}
if (curSeond == null ){
temp.next = curFirst;
}
return head;
}