zoukankan      html  css  js  c++  java
  • 328. Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

    You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

    Example:
    Given 1->2->3->4->5->NULL,
    return 1->3->5->2->4->NULL.

    Note:
    The relative order inside both the even and odd groups should remain as it was in the input. 
    The first node is considered odd, the second node even and so on ...

    public ListNode OddEvenList(ListNode head) {
            if(head == null) return head;
            ListNode odd  = head;
            ListNode even = head.next;
            ListNode stand = head.next;
            while((odd.next != null)&&(odd.next.next != null))
            {
                odd.next = odd.next.next;
                if(even.next != null)
                {
                even.next = even.next.next;    
                even  = even.next;
                }
                
                odd = odd.next;
                
            }
            odd.next  = stand;
            return head;
            
        }
  • 相关阅读:
    Linux搭建ElasticSearch环境
    从DDD开始说起
    TFS看板晨会
    TFS看板的迭代规划
    TFS看板规则
    TFS看板的设计
    Api容器在应用架构演化中的用途
    基于Abp的WebApi容器
    线程队列
    动态类型序列化
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5856058.html
Copyright © 2011-2022 走看看