Odd Even Linked List

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 …

/**
 * 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) {

    }
};

我的解法

​ 两个头结点指针和两个尾结点指针,分别为奇数(odd)链和偶数(even)链,用num保存第一个数的值,来判断奇数链在前还是偶数链在前。然后将根据num的值将两个链表连接起来。其实这里有两处错误。

Leetcode错误信息

Runtime Error Message:Line 22: member access within null pointer of type ‘struct ListNode’

Last executed input:[]

错误代码为:

int num = head -> val;

原因是对空指针类型的成员访问

第二个错误原因

Please note here we are talking about the node number and not the value in the nodes.

译为:请注意,我们说的是结点数而不是结点的值。

开始的时候我没有明白是什么意思,按照错误的方式做了一遍。

这么看来,就没有必要开一个int num按照错误的做法,发现了一个错误 :p

正确代码如下:

ListNode* oddEvenList(ListNode* head) {
    ListNode *oddPtr, *evenPtr, *oddhead, *evenhead, *curPtr;
    oddPtr = (ListNode *)malloc(sizeof(ListNode));
    oddhead = oddPtr -> next = NULL;
    evenPtr = (ListNode *)malloc(sizeof(ListNode));
    evenhead = evenPtr -> next = NULL;
    int oddflag = 0, evenflag = 0, kase = 1;
    while(head){
        curPtr = (ListNode *)malloc(sizeof(ListNode ));
        curPtr -> val = head -> val;
        curPtr -> next = NULL;
        if(kase % 2){
            if(!oddflag){
                oddhead = curPtr;
                oddflag = 1;
            }
            oddPtr -> next = curPtr;
            oddPtr = curPtr;
        }
        else {
            if(!evenflag){
                evenhead = curPtr;
                evenflag = 1;
            }
            evenPtr -> next = curPtr;
            evenPtr = curPtr;
        }
        kase ++;
        head = head -> next;
    }
    oddPtr -> next = evenhead;
    return oddhead;
}

自己测试写的其他部分:

#include <cstdio>
#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct ListNode {
    int val;
    ListNode *next;
}ListNode;
int main(void)
{

    ListNode *curPtr, *headPtr, *rearPtr;
    headPtr = (ListNode *)malloc(sizeof(ListNode));
    headPtr -> next = NULL;
    rearPtr = headPtr;
    int T;
    cin >> T;
    while(T--){
        curPtr = (ListNode *)malloc(sizeof(ListNode));
        curPtr -> next = NULL;
        cin >> curPtr -> val;
        rearPtr -> next = curPtr;
        rearPtr = curPtr;
    }
    headPtr = oddEvenList(headPtr -> next);
    while(headPtr){
        printf(" %d", headPtr -> val);
        headPtr = headPtr -> next;
    }
    return 0;
}

不得不说做得很慢,而且英语也真的菜,没看懂就开始敲了。

我的做法很繁琐,这是LeetCode的Discuss里别人的代码:

 ListNode* oddEvenList(ListNode* head) 
    {
        if(!head) return head;
        ListNode *odd=head, *evenhead=head->next, *even = evenhead;
        while(even && even->next)
        {
            odd->next = odd->next->next;
            even->next = even->next->next;
            odd = odd->next;
            even = even->next;
        }
        odd->next = evenhead;
        return head;
    }

给跪了。。ORZ

思路差不多,但是代码比我简洁很多。

可能也是有——平常做题,自己习惯写带头结点的链表的原因

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值