分析
1.传递两个链表参数
2.创建辅助变量存储第一个链表,防止第一个链表的头节点发生变化
3.创建辅助变量存储第二个链表的下一个节点,防止在改变当前节点的指针时,导致后续节点失效
4.遍历第二个链表,在遍历过程中遍历第一个链表,判断节点的排序
代码实现
public static HeroNode betweenLinkedList(HeroNode start,HeroNode end){
if (start.next == null){
return end;
}
if (end.next == null){
return start;
}
HeroNode insert = end.next;
HeroNode head = start;
HeroNode next = null;
while (insert != null){
next = insert.next;
while (true){
if (head.next == null){
insert.next = null;
head.next = insert;
break;
}
if (head.next.no >= insert.no){
insert.next = head.next;
head.next = insert;
break;
}
head = head.next;
}
insert = next;
}
return start;
}
public HeroNode getHead() {
return head;
}
}