美文网首页
328. Odd Even Linked List

328. Odd Even Linked List

作者: namelessEcho | 来源:发表于2017-09-27 15:15 被阅读0次

两根指针,最后把even的指针append到odd的末尾。

 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode oddEvenList(ListNode head) {
        if(head==null||head.next==null)return head;
        ListNode odd = head ;
        ListNode even = head.next;
        ListNode temp =even;
       while(even!=null&&even.next!=null)
       {
           odd.next=even.next;
           odd=odd.next;
           even.next=even.next.next;
           even=even.next;
       }
        odd.next=temp;
        return head;
    }
}

相关文章

网友评论

      本文标题:328. Odd Even Linked List

      本文链接:https://www.haomeiwen.com/subject/nyyuextx.html