zoukankan      html  css  js  c++  java
  • Add to List 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 ...

    class Solution {
    public:
    ListNode* oddEvenList(ListNode* head) {
    //
    if(head==nullptr)return head;
    if(head->next == nullptr)return head;
    ListNode* H = head;
    ListNode* start;
    auto mark = start;
    int count = 0;
    start = H->next;
    mark = start;
    ///注意点一,链表结构变化后不要认为此链表指针没有变化
    ///注意点二,null指针不能使用next H->next,一定要判断H是否位nullptr,包括H->next->next;
    ///注意点三,特殊例子与一般的例子的驱动,特例驱动写出if排除,一般代表性的例子写一般程序
    while(H)
    {
    if(H->next==nullptr)break;
    if(H->next->next==nullptr)break;
    H->next = H->next->next;
    H = H->next;
    start->next = start->next->next;
    start = start->next;
    }
    H->next = mark;
    return head;
    }
    };

  • 相关阅读:
    J. 最大权边独立集 题解(树上背包)
    F. 地图压缩 题解(kmp 最小循环节)
    Sum of Log 题解(数位dp)
    F. Scalar Queries 题解(思维+线段树)
    B. 攻防演练 题解(思维+倍增)
    Bit Sequence 题解(数位dp)
    机器学习
    Android学习笔记
    sqoop
    Initialization failed for block pool Block pool
  • 原文地址:https://www.cnblogs.com/fenglongyu/p/7761038.html
Copyright © 2011-2022 走看看