-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOdd Even Linked List.cpp
47 lines (44 loc) · 1.04 KB
/
Odd Even Linked List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* 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) {
ListNode *temp=head;
int len=1;
if(head==NULL)
return head;
while(temp->next!=NULL)
{
temp=temp->next;
len+=1;
}
cout<<"len: "<<len<<"\n";
int ct=1;
ListNode *previous=head,*tobe=head;
while(ct<=len && len!=2)
{
cout<<ct<<"\n";
if(ct%2 ==0)
{
previous->next=head->next;
temp->next=head;
head->next=NULL;
temp=temp->next;
head=previous->next;
}
else
{
previous=head;
head=head->next;
}
ct+=1;
}
return tobe;
}
};