ok 边界头划分错了
在这里插入代码片
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode merge(ListNode phead1,ListNode phead2){
if(phead1==null){
return phead2;
}
if(phead2==null){
return phead1;
}
ListNode head=new ListNode(0);
ListNode cur=head;
while(phead1!=null&&phead2!=null)
{if(phead1.val<=phead2.val){
cur.next=phead1;
phead1=phead1.next;//phead1 定位后一 当前1链表
}else{
cur.next=phead2;
phead2=phead2.next;//phead2 为了下次连接 定位到下个比较节点 类似1
}
cur=cur.next;//cur定位到新链表 此次循环内定位 新加节点 以便下次连接
}
if(phead1!=null){
cur.next=phead1;
// cur=cur.next;//没定位 之前 晕 不定位的话 好像也行 看眼答案
}
else
{ cur.next=phead2;
}
return head.next;
}
public ListNode sortInList (ListNode head) {
// write code here
if(head==null||head.next==null){
return head;
}
ListNode fast=head.next.next;
ListNode slow=head.next;
ListNode prev=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
prev=prev.next;
}
prev.next=null;
return merge(sortInList(head),sortInList(slow));
}
}
注意 定义数组下标 应该用int 而不是用节点定义
看了一眼