odd-even-linked-list 1.0.0
Odd Even Linked List
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

ListNodeoddEvenList (ListNode *head)
 

Detailed Description

Definition at line 17 of file main.cpp.

Member Function Documentation

◆ oddEvenList()

ListNode * Solution::oddEvenList ( ListNode head)
inline

Definition at line 20 of file main.cpp.

21 {
22 if (!head)
23 return NULL;
24 list<ListNode*> odd;
25 list<ListNode*> even;
26 ListNode* current = head;
27 bool is_odd = true;
28 while (current != NULL)
29 {
30 if (is_odd)
31 odd.push_back(current);
32 else
33 even.push_back(current);
34 current = current->next;
35 is_odd = !is_odd;
36 }
37
38 current = head;
39 auto it = odd.begin();
40 it++;
41 for (; it != odd.end(); ++it)
42 {
43 current->next = *it;
44 current = current->next;
45 }
46 it = even.begin();
47 for (; it != even.end(); ++it)
48 {
49 current->next = *it;
50 current = current->next;
51 }
52 current->next = NULL;
53 return head;
54 }
Definition for singly-linked list.
Definition main.cpp:9
ListNode * next
Definition main.cpp:11

References ListNode::next.


The documentation for this class was generated from the following file: