这个题没啥难的注意边界条件就好
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Solution {
public ListNode oddEvenList(ListNode head) {
if(head==null)
{
return null;
}
if(head.next==null)
{
return head;
}
boolean isodd=true;
ListNode odd=head;
ListNode even=head.next;
ListNode oddhead=head;
ListNode evenhead=head.next;
head=head.next.next;
while(head!=null)
{
if(isodd)
{
odd.next=head;
odd=odd.next;
}
else
{
even.next=head;
even=even.next;
}
isodd=!isodd;
}
odd.next=evenhead;
even.next=null;
return oddhead;
}
}
本文介绍了一种链表重组算法,该算法将链表中的节点按奇偶顺序重新排列。通过遍历链表并使用两个指针分别跟踪奇数位置和偶数位置的节点来实现这一目标。最后,将奇数部分与偶数部分连接起来形成新的链表。
574

被折叠的 条评论
为什么被折叠?



