# ODD-EVEN-LINKED-LIST leetcode Solution - Correct, Optimal and Working

``````/*
*  Author: Arpit Bhayani
*  https://arpitbhayani.me
*/
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <iostream>
#include <list>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

#define ll long long

#define MIN(a, b) a < b ? a : b
#define MAX(a, b) a > b ? a : b

using namespace std;

int readline(char *str) {
int i = 0;
char ch;
while((ch = getchar()) != '\n') {
str[i++] = ch;
}
str[i] = '\0';
return i;
}

struct node {
int val;
struct node * next;
};

struct node * new_node(int d) {
struct node * t = (struct node *) calloc(1, sizeof(struct node));
t->val = d;
return t;
}

void printList(struct node * head) {
struct node * p = head;
while(p) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}

struct node * oddEvenList(struct node * head) {
if(head == NULL) {
return NULL;
}
struct node * odd_head = NULL, * even_head = NULL;
struct node * odd_tail = NULL, * even_tail = NULL;
struct node * odd_tail_prev = NULL;

while(odd_tail && even_tail) {
if(odd_tail->next) {
odd_tail->next = odd_tail->next->next;
}

if(even_tail->next) {
even_tail->next = even_tail->next->next;
}

odd_tail_prev = odd_tail;
odd_tail = odd_tail->next;
even_tail = even_tail->next;
}

if(odd_tail) {
}
else {
}
}

int main(int argc, char *argv[]) {
struct node * head = new_node(1);