数据结构复习---leetcode328 odd even linked list

链表的题目,一定要想明白了再写,否则会越写越乱,想明白了很简单( •̀ ω •́ )✧

    public ListNode oddEvenList(ListNode head) {
    	if(head==null)
    		return head;
        ListNode p=head,q=head.next,r=p,s=q;
        while(p!=null&&q!=null) {
        	if(q.next==null) 
        		break;
        	p.next=q.next;
        	p=p.next;
        	q.next=p.next;
        	q=q.next;
        }
        p.next=s;
        return r;
    }

1ms beats 100%

猜你喜欢

转载自blog.csdn.net/cobracanary/article/details/88953991