public class Solution {
/**
* 按照左右半区的方式重新组合单链表
* 输入:一个单链表的头节点head
* 将链表调整成题目要求的样子
*
* 后台的单链表节点类如下:
*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public static void relocateList(ListNode head) {
//1.快慢指针找到中间点
ListNode hh = new ListNode(-1);//没分清“头部节点”的补救措施
hh.next = head;
ListNode l1,lmid,locate;//locate定位节点
l1 = hh;
lmid = hh;
int length = 0;
for(;l1.next!=null;l1 = l1.next){
length++;
if(length%2 == 0)
lmid = lmid.next;
}
if(length > 3) {
locate = lmid;
lmid = lmid.next;//lmid及以后的都是R
locate.next = null;
l1 = hh.next;
while (l1 != null) {
locate = l1;
l1 = l1.next;
locate.next = lmid;
locate = lmid;
if (l1 != null) {
lmid = lmid.next;
locate.next = l1;
}
}
}
}
}
反正过了。。。