From dcd73ae26c9e23453e2c7e0096397da157f3f103 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Sun, 17 May 2020 10:34:20 +0530 Subject: [PATCH] Solution to #16 --- Solutions/S16.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Solutions/S16.cpp diff --git a/Solutions/S16.cpp b/Solutions/S16.cpp new file mode 100644 index 0000000..01df041 --- /dev/null +++ b/Solutions/S16.cpp @@ -0,0 +1,26 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ + +ListNode* oddEvenList(ListNode* head) { + if(!head) return NULL; + else { + ListNode *odd = head, *even = head->next, *evenHead = even; + while(even && even->next) + { + odd->next = even->next; + odd = odd->next; + even->next = odd->next; + even = even->next; + } + odd->next = evenHead; + return head; + } +} \ No newline at end of file