Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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) {}
- * };
- */
- struct Linked_List {
- ListNode *head = NULL;
- ListNode *tail = NULL;
- int num_elements = 0;
- void push(int value) {
- ListNode *new_node = new ListNode;
- new_node->val = value;
- new_node->next = NULL;
- if (head == NULL) {
- head = new_node;
- tail = head;
- } else {
- tail->next = new_node;
- tail = tail->next;
- }
- num_elements++;
- }
- };
- class Solution {
- public:
- ListNode* oddEvenList(ListNode* head) {
- if(head == NULL)return head;
- Linked_List Odds;
- Linked_List Evens;
- ListNode *current = NULL;
- current = head;
- int i = 0;
- while(current){
- if(i%2==0)Odds.push(current->val);
- else Evens.push(current->val);
- i++;
- current = current->next;
- }
- Odds.tail->next = Evens.head;
- return Odds.head;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement