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

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* oddEvenList(ListNode* head) {
            if(head == NULL || head->next == NULL || head->next->next == NULL) return head;
            ListNode* oddDummy = new ListNode(0);        //为了方便写while循环,这里设置两个头结点
            ListNode* evenDummy = new ListNode(0);
            ListNode* oddList = oddDummy;
            ListNode* evenList = evenDummy;
            int i = 1;
            while(head != NULL){
                if(i % 2 == 1){
                    oddList->next = head;
                    oddList = oddList->next;
                }else{
                    evenList->next = head;
                    evenList = evenList->next;
                }
                i++;
                ListNode* tmp = head->next;
                head->next = NULL;
                head = tmp;
            }
            oddList->next = evenDummy->next;
            return oddDummy->next;
        }
    };
    
  • 相关阅读:
    HTML基础 整理
    今天课堂总结
    课后习题
    JAVA的文件创建
    JAVA_输入输出流 异常处理
    12.23流水账号
    12.22 repeater 删除
    12.22 repeater 修改
    12.22 repeater 添加
    12.22 repeater 主页
  • 原文地址:https://www.cnblogs.com/UniMilky/p/7160396.html
Copyright © 2011-2022 走看看