Wednesday, November 20, 2013

Day 52, 25, Reverse Nodes in k-Group

Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
---------------------------------------------------------------------
Iterative, similar toDay 42, #92 Reverse Linked List II
How to tackle this using recursion? Later !!!
-- relax, it is easy,

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if (k == 1) return head;
        if (head == NULL || head->next == NULL) return head;
        ListNode *newHead = head,*itr = head, *tail = NULL;
        int count = 0;
        while(itr != NULL) {
            itr = itr->next;
            count++;
            if (count == k) {
                ListNode* newTail = newHead, *itr2 = newHead;
                newHead = tail;
                while (count > 0) {
                    ListNode* next = itr2->next;
                    itr2->next = newHead;
                    newHead = itr2;
                    itr2 = next;
                    count--;
                }
                if (tail == NULL) {
                    head = newHead;
                }else  tail->next = newHead;
                newTail->next = itr2;
                tail = newTail;
                newHead = itr2;
            }
        }
        return head;
    }
};
Update on Feb-06-2015
Using dummy node
class Solution {
public:
    ListNode *reverseKGroup(ListNode *head, int k) {
        if (k <= 1 || head == NULL) return head;
        ListNode *dummy = new ListNode(0);
        dummy->next = head;
        ListNode *pre = dummy;
        
        int count = 0;
        while (head != NULL) {
            count++;
            
            if (count == k) {
                // reverse
                ListNode *newHead = NULL, *itr = pre->next, *tempNext = head->next;
                while (itr != tempNext) {
                    ListNode *temp = itr->next;
                    itr->next = newHead;
                    newHead = itr;
                    itr = temp;
                }
                ListNode *tail = pre->next;
                pre->next = newHead;
                pre = tail;
                tail->next = tempNext;
                head = tempNext;
                count = 0;
            }else
                head = head->next;
        }
        
        return dummy->next;
    }
};

No comments:

Post a Comment