19 - Remove Nth Node from End of List

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* h = new ListNode(0);
        h->next = head;

        ListNode* first = h;
        for(int i=0;i<n && first!=NULL;++i)
            first = first->next;

        ListNode* second = h;
        while(first->next!=NULL) {
            first = first->next;
            second = second->next;
        }

        ListNode* toBeDelete = second->next;
        second->next = toBeDelete->next;
        delete toBeDelete;

        return h->next;
    }
};

234 - Palindrome Linked ListNode

21 - Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode head(0);
        ListNode * cur = &head;

        ListNode *i = l1, *j = l2;
        while(i != NULL && j != NULL) {
            if(i->val < j->val) {
                cur->next = i;
                i = i->next;
            }
            else {
                cur->next = j;
                j = j->next;
            }
            cur = cur->next;
        }

        if(i == NULL) {
            cur->next = j;
        }
        else {
            cur->next = i;
        }

        return head.next;
    }
};

23 - Merge k Sorted Lists

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode head(0);
        ListNode *cur = &head;

        priority_queue<ListNode*, vector<ListNode*>, cmp> que;

        for(int i=0;i<lists.size();++i) {
            if(lists[i] != NULL)
                que.push(lists[i]);
        }

        ListNode * tmp;
        while(!que.empty()) {
            tmp = que.top();
            que.pop();
            cur->next = tmp;
            cur = cur->next;

            if(tmp->next != NULL)
                que.push(tmp->next);
        }

        cur->next = NULL;

        return head.next;
    }

    struct cmp {
        bool operator()(ListNode *l1, ListNode *l2) {
            return l1->val > l2->val;
        }
    };
};

24 - Swap Nodes in Pairs

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode empty(0);
        empty.next = head;

        ListNode *cur = &empty;
        ListNode *first, *second;

        while(cur != NULL) {
            first = cur->next;
            if(first == NULL) break;
            second = first->next;
            if(second == NULL) break;

            cur->next = second;
            first->next = second->next;
            second->next = first;

            cur = first;
        }

        return empty.next;
    }
};

25 - Reverse Nodes in k-Groups

/**
 * 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) {
        if(head == NULL || k <= 1) return head;

        ListNode empty(0);
        ListNode* tail = &empty;
        tail->next = head;

        while(true) {
            int c = 0;
            ListNode* nextHead = head;
            while(nextHead != NULL && c < k) {
                nextHead = nextHead->next;
                ++c;
            }

            if(c < k) break;

            ListNode* newTail = head;
            ListNode* toInsert = head->next;

            for(int i=0;i<k-1;++i) {
                ListNode* tmp = toInsert->next;
                toInsert->next = tail->next;
                tail->next = toInsert;
                toInsert = tmp;
            }

            tail = newTail;
            head = nextHead;
            tail->next = head;
        }

        return empty.next;
    }
};


blog comments powered by Disqus

Published

26 June 2016

Tags