题解 | #链表的奇偶重排#
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode oddEvenList (ListNode head) {
if(head == null || head.next == null || head.next.next == null){
return head;
}
ListNode oHead = head.next, jCur = head, oCur = oHead;
while(oCur.next != null && oCur.next.next != null){
jCur.next = jCur.next.next;
jCur = jCur.next;
oCur.next = oCur.next.next;
oCur = oCur.next;
System.out.println(jCur.val);
}
//链表数量为奇数的时候,最后还有个奇数位节点
if(oCur.next != null){
jCur.next = oCur.next;
//大坑啊,偶数位最后一个要与最后一个奇数位断开,一定要断开,不然就成环了。
oCur.next = null;
jCur = jCur.next;
}
jCur.next = oHead;
return head;
}
}
#学习##刷题##每日刷题#

