首页 > 电脑

这个哪里错了,麻烦大神帮我看看

更新时间2020-01-22 01:22:53

//合并两个有序的单链表,合并之后的链表依然有序

public SingleLinkedListhw MergeList(SingleLinkedListhw A,SingleLinkedListhw B) {

if(A.head.next == null && B.head.next == null) {

return null;

}else if(A.head.next == null) {

return B;

}else if(B.head.next == null) {

return A;

}

 

SingleLinkedListhw C = new SingleLinkedListhw();

HeroNodehw cursor1 = A.head.next;

HeroNodehw cursor2 = B.head.next;

 

while(cursor1 != null && cursor2 != null) {

if(cursor1.no <= cursor2.no) {

C.add(cursor1);

cursor1 = cursor1.next;

}else{

C.add(cursor2);

cursor2 = cursor2.next;

}

}

 

while(cursor1 != null) {

C.add(cursor1);

cursor1 = cursor1.next;

}

 

while(cursor2 != null) {

C.add(cursor2);

cursor2 = cursor2.next;

}

 

return C;

 

}


可以在链表最后加两个哨兵(inf)试试

相关标签:大神

上一篇:GeForceGTX1660

下一篇:如何输入char类型