一、算法分类经典题目练习:链表

206.反转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    ListNode* reverseList(ListNode* head) {
     
        ListNode* pre = NULL;
        ListNode* current = head;
        ListNode* tmp;
        while(current != NULL){
     
            tmp = current->next;
            current->next = pre;
            pre = current;
            current = tmp;
        }
        return pre;
    }
};

92.反转链表II

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
     
        int change_len = n - m + 1;
        ListNode* pre_head = NULL;
        ListNode* result = head;
        while(head&&--m){
     
            pre_head = head;
            head = head->next;
        }
        ListNode* modify_list_tail = head;
        ListNode* new_head = NULL;
        while(head&&change_len){
     
            ListNode* next = head->next;
            head->next = new_head;
            new_head = head;
            head = next;
            change_len--;
        }
        modify_list_tail->next = head;
        if(pre_head){
     
            pre_head->next = new_head;
        }else{
     
            result = new_head;
        }
        return result;
    }
};

160.相交链表
<1>使用set

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
     
        set<ListNode*> node_set;
        while(headA){
     
            node_set.insert(headA);
            headA = headA->next;
        }
        while(headB){
     
            if(node_set.find(headB)!=node_set.end())
                return headB;
            headB = headB->next;
        }
        return NULL;
    }
};

<2>衔接遍历,返回第一个相同节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
     
        if(headA==NULL || headB==NULL)
            return NULL;    
        ListNode* l1 = headA, *l2 = headB;
        while(l1!=l2){
     
            if(l1)
                l1 = l1->next;
            else
                l1 = headB;
            if(l2)  
                l2 = l2->next;
            else    
                l2 = headA;
        }
        return l1;
    }
};

<3>对齐求解

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
     
        if(headA==NULL || headB==NULL)
            return NULL;
        ListNode* pa, *pb;
        int countA = getLen(headA);
        int countB = getLen(headB);
        for(pa = headA; countA > countB; countA--){
     
            pa = pa->next;
        }
        for(pb = headB; countA < countB; countB--){
     
            pb = pb->next;
        }
        while(pa!=NULL && pa!=pb){
     
            pa = pa->next;
            pb = pb->next;
        }
        return pa;
    }
    int getLen(ListNode* head){
     
        int len = 1;
        while(head->next) len++,head=head->next;
        return len;
    }
};

141.环形链表
<1>使用set

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    bool hasCycle(ListNode *head) {
     
        set<ListNode*> node_set;
        while(head){
     
            if(node_set.find(head)!=node_set.end())
                return true;
            node_set.insert(head);
            head = head->next;
        }
        return false;
    }
};

<2>快慢指针

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    bool hasCycle(ListNode *head) {
     
        ListNode* fast = head;
        ListNode* slow = head;
        while(fast!=NULL && fast->next!=NULL && slow!=NULL){
     
            fast = fast->next->next;
            slow = slow->next;
            if(fast==slow) return true;
        }
        return false;
    }
};

86.分割链表
临时头节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    ListNode* partition(ListNode* head, int x) {
     
        ListNode less_head(0);
        ListNode more_head(0);
        ListNode* less_ptr = &less_head;
        ListNode* more_ptr = &more_head;
        while(head){
     
            if(head->val < x){
     
                less_ptr->next = head;
                less_ptr = head;
            }else{
     
                more_ptr->next = head;
                more_ptr = head;
            }
            head = head->next;
        }
        less_ptr->next = more_head.next;
        more_ptr->next = NULL;
        return less_head.next;
    }
};

138.复制带随机指针的链表
深拷贝

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* next;
    Node* random;
    
    Node(int _val) {
        val = _val;
        next = NULL;
        random = NULL;
    }
};
*/

class Solution {
     
public:
    Node* copyRandomList(Node* head) {
     
        map<Node*, int> node_map;
        vector<Node*> node_vec;
        Node* ptr = head;
        int i = 0;
        while(ptr){
     
            node_vec.push_back(new Node(ptr->val));
            node_map[ptr] = i;
            ptr = ptr->next;
            i++;
        }
        node_vec.push_back(0);
        i = 0;
        ptr = head;
        while(ptr){
     
            node_vec[i]->next = node_vec[i+1];
            if(ptr->random){
     
                int id = node_map[ptr->random];
                node_vec[i]->random = node_vec[id];
            }
            ptr = ptr->next;
            i++;
        }
        return node_vec[0];
    }
};

21.合并两个有序链表
<1>临时头节点

/**
 * 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* mergeTwoLists(ListNode* l1, ListNode* l2) {
     
        ListNode temp(0);
        ListNode* pre = &temp;
        while(l1&&l2){
     
            if(l1->val < l2->val){
     
                pre->next = l1;
                l1 = l1->next;
            }else{
     
                pre->next = l2;
                l2 = l2->next;
            }
            pre = pre->next;
        }
        if(l1){
     
            pre->next = l1;
        }
        if(l2){
     
            pre->next = l2;
        }
        return temp.next;
    }
};

<2>递归

/**
 * 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* mergeTwoLists(ListNode* l1, ListNode* l2) {
     
        if(l1==NULL){
     
            return l2;
        }else if(l2 == NULL){
     
            return l1;
        }else{
     
            if(l1->val < l2->val){
     
                l1->next = mergeTwoLists(l1->next, l2);
                return l1;
            }else{
     
                l2->next = mergeTwoLists(l1, l2->next);
                return l2;
            }
        }
    }
};

23.合并K 个升序链表
使用vector排序

/**
 * 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* mergeKLists(vector<ListNode*>& lists) {
     
        vector<ListNode*> node_vec;
        for(int i = 0; i < lists.size(); ++i){
     
            ListNode* head = lists[i];
            while(head){
     
                node_vec.push_back(head);
                head = head->next;
            }
        }
        if(node_vec.size()==0) return NULL;
        sort(node_vec.begin(), node_vec.end(), cmp);
        for(int i = 0; i < node_vec.size()-1; ++i){
     
            node_vec[i]->next = node_vec[i+1];
        }
        node_vec[node_vec.size()-1]->next = NULL;
        return node_vec[0];
    }
    static bool cmp(const ListNode* a, const ListNode* b){
     
        return a->val < b->val;
    }
};

你可能感兴趣的:(算法分类练习,leetcode,算法,链表)