Odd Even Linked List Solutions in C++
Number 328
Difficulty Medium
Acceptance 55.8%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/odd-even-linked-list/// Author : Hao Chen// Date : 2016-01-16/*** 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) {if (!head) return head;ListNode* pOdd = head;ListNode* p = head->next;ListNode* pNext = NULL;while(p && (pNext=p->next)) {p->next = pNext->next;pNext->next = pOdd->next;pOdd->next = pNext;p = p->next;pOdd = pOdd->next;}return head;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/odd-even-linked-list/description//// Author : liuyubobobo/// Time : 2018-10-01#include <iostream>using namespace std;/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// Split the Linked List into two and then merge/// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:ListNode* oddEvenList(ListNode* head) {if(head == NULL || head->next == NULL || head->next->next == NULL)return head;ListNode* dummyHead1 = new ListNode(-1);ListNode* dummyHead2 = new ListNode(-1);ListNode* p1 = dummyHead1;ListNode* p2 = dummyHead2;ListNode* p = head;for(int i = 0; p; i ++)if(i % 2 == 0){p1->next = p;p = p->next;p1 = p1->next;p1->next = NULL;}else{p2->next = p;p = p->next;p2 = p2->next;p2->next = NULL;}p1->next = dummyHead2->next;ListNode* ret = dummyHead1->next;delete dummyHead1;delete dummyHead2;return ret;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/odd-even-linked-list/description//// Author : liuyubobobo/// Time : 2018-10-01#include <iostream>using namespace std;/// Definition for singly-linked list.struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};/// Split the Linked List into two and then merge/// Keep one in original Linked List////// Time Complexity: O(n)/// Space Complexity: O(1)class Solution {public:ListNode* oddEvenList(ListNode* head) {if(head == NULL || head->next == NULL || head->next->next == NULL)return head;ListNode* dummyHead2 = new ListNode(-1);ListNode* p2 = dummyHead2;ListNode* p = head;while(p->next){p2->next = p->next;if(p->next->next == NULL){p->next = NULL;break;}p->next = p->next->next;p = p->next;p2 = p2->next;p2->next = NULL;}p->next = dummyHead2->next;delete dummyHead2;return head;}};int main() {return 0;}