题解 | #链表的奇偶重排#
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
链表的奇偶重排
https://www.nowcoder.com/practice/02bf49ea45cd486daa031614f9bd6fc3
public ListNode oddEvenList (ListNode head) {
if (head==null||head.next==null){
return null;
}
ListNode a=head;//单节点的头结点
ListNode b=head.next;//双节点的头节点
ListNode a1=a;//用a1去遍历链表中所有单节点
ListNode b1=b;//用b1去遍历链表中所有双节点
while (a1.next!=null&&b1.next!=null){
a1.next=b1.next;
a1=a1.next;
b1.next=a1.next;
b1=b1.next;
}
a1.next=b;//将单节点与双节点进行链接
return a;
}
以上
相关推荐