Reconnect the Nodes in Linked List by Odd/Even in Place (Odd Eve

  • 时间:2020-10-07 14:14:07
  • 分类:网络文摘
  • 阅读:137 次

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example 1:

Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL

Example 2:
Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULL

Constraints:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on …
The length of the linked list is between [0, 10^4].

Construct Odd Even Linked List using Additional Space

The easiest algorithm to rearrange the nodes would be to traverse the linked list and copy the node to two linked list even and odd. Then at the end, we connect the head of the even to the end of the odd And return the new head.

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
/**
 * 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) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        if (!head) return nullptr;
        ListNode* dummyOdd = new ListNode(-1);
        ListNode* dummyEven = new ListNode(-1);
        int i = 1;
        ListNode* odd = dummyOdd;
        ListNode* even = dummyEven;
        while (head) {
            if ((i & 1) == 1) {
                odd->next = new ListNode(head->val);
                odd = odd->next;
            } else {
                even->next = new ListNode(head->val);
                even = even->next;
            }
            head = head->next;
            i ++;
        }
        odd->next = dummyEven->next;
        return dummyOdd->next;
    }
};
/**
 * 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) {}
 * };
 */
class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        if (!head) return nullptr;
        ListNode* dummyOdd = new ListNode(-1);
        ListNode* dummyEven = new ListNode(-1);
        int i = 1;
        ListNode* odd = dummyOdd;
        ListNode* even = dummyEven;
        while (head) {
            if ((i & 1) == 1) {
                odd->next = new ListNode(head->val);
                odd = odd->next;
            } else {
                even->next = new ListNode(head->val);
                even = even->next;
            }
            head = head->next;
            i ++;
        }
        odd->next = dummyEven->next;
        return dummyOdd->next;
    }
};

The space requirement is O(N) as we are allocating the N copies of the nodes. The time complexity is O(N) as we are iterating once from the head to the end of the linked list.

Exchanging the Nodes in-place to Odd/Even Linked List

We can have two pointers – odd and even, and then move them forward and re-connect the nodes while we are traversing the linked list. See below C++ code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
 * 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 == nullptr) return NULL;
        auto 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;
    }
};
/**
 * 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 == nullptr) return NULL;
        auto 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;
    }
};

We connect nodes of odd positions and nodes of even positions separately and then at the end, we append the even pointer to the end of the odd linked list. This approach only requires O(1) constant space – and the time performance is O(N).

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
5 Tools That Will Grow Your Online Retail Business  #PlaylistsForLife: Doctors Without Borders Launches Awareness Ca  Setting up WordPress to Run a User-Driven Site  Online Multiplayer Game Goes Viral By Reinventing A Classic  5 Things Beginning Bloggers Struggle With the Most  Win the “Integral” WordPress Theme from Themely  7 Habits of Highly Productive Bloggers  How a Business Owner Builds Relationships With Influential Blogg  5 Promising Tools to Help Boost Your Online Startup  How to For-Loop and do Math/Arithmetic Operations in Windows/NT  
评论列表
添加评论