第4章:LeetCode--链表

2. Add Two Numbers:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* ret, *tmp;
        int carry = 0, sum=0;
        ret = tmp = new ListNode(0);
        while(carry||l1||l2){
            sum = carry;
            if(l1)sum+=l1->val;
            if(l2)sum+=l2->val;
            carry = sum/10;
            tmp->val = sum%10;
            if(l1)l1 = l1->next?l1->next:NULL;
            if(l2)l2 = l2->next?l2->next:NULL;
            if(!l1 && !l2 && !carry)return ret;
            tmp->next = new ListNode(0);
            tmp=tmp->next;
        }
        return NULL;
    }
};

 

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) {
        if(head == NULL || n == 0) return head;
        ListNode* slow = head;
        ListNode* fast = head;
        ListNode* pre = head;
        if(fast->next == NULL) return NULL; //[1] 1
        while(n>0){ //if n, slow will just point to the nth
            fast = fast->next;
            n--;
        }
        while(fast != NULL){
            fast = fast->next;
            pre = slow;
            slow = slow->next;
        }
        pre->next = slow->next;
        //if pre==slow==head [1,2] 2
        if(pre == slow) return head->next;
         
        return head;
    }
};

  

21. Merge Two Sorted Lists

ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
     ListNode *retList = NULL, *tempList = NULL;
     if(!l1 && !l2) return retList;
     int val = 0, val1 = 0, val2 = 0;
     retList = tempList = new ListNode(0);
     while(l1 || l2){
         val1 = l1?l1->val:0;
         val2 = l2?l2->val:0;
         if(((val1<val2) && l1) || !l2){
             tempList->val = val1;
             if(!(l1->next) && !l2) return retList;
             tempList->next = new ListNode(0);
             tempList = tempList->next;
             l1 = (l1->next)?l1->next:NULL;
             continue;
         }
         else{
             tempList->val = val2;
             if(!(l2->next) && !l1) return retList;
              
             tempList->next = new ListNode(0);
             tempList = tempList->next;
             l2 = (l2->next)?l2->next:NULL;
             continue;     
         }
     }
     return retList;
}
//Other guy's solution
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
{
    if(!l1)         // If no l1, return l2
        return l2;
    if(!l2)         // If no l2, return l1
        return l1;
    if(!l2 && !l1)  // If neither, return NULL;
        return NULL;
         
    ListNode* head; // The pointer we will use to construct a merged list
     
     
    if(l1->val < l2->val)   // If l1 less than l2
    {
        head = l1;          // We start at l1
        l1 = l1->next;      // and iterate l1
    }
    else                    // If l2 less than l1
    {
        head = l2;          // We start at l2
        l2 = l2->next;      // and iterate l2
    }
     
    ListNode* ret = head;   // We need to save the addres of the head of the list
     
    while(l1 && l2)         // While both input lists have values
    {
        if(l1->val < l2->val)   // Compare the current values, if l1 is less
        {
            head->next = l1;    // Append the merged list with l1's current address
            l1 = l1->next;      // Advance l1
        }
        else                    // Else, l2 had the low value
        {
            head->next = l2;    // Append l2 to the list
            l2 = l2->next;      // Advance l2
        }
        head->next->next = NULL;    // Append a NULL teminator to the list
        head = head->next;          // Advance the merged list
    }
     
    // Lastly, if list were different lengths, we need to append the longer list tail to the merged list
     
    if(l1)
        head->next = l1;
    else if(l2)
        head->next = l2;
         
    return ret; // Return the starting address of head that we saved.
}

  

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* first = head;
        if(head == NULL || head->next == NULL)return head;
        ListNode* second = head->next;
        ListNode* ret = second;
        ListNode* third = second->next;
        
        second->next = first;
        
        while(third && third->next){
            first->next = third->next;
            first = third;
            second = third->next;
            third = second->next;
            second->next = first;
        }
        if(third == NULL){
            second->next = first;
            first->next = NULL;
        }else{
            //third->next =NULL
            first->next = third;
        }
        return ret;
    }
};

 

61. Rotate List

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head == NULL || k==0) return head;
        int len = 1;
        ListNode* orighead = head;
        ListNode* newhead=head;
        while(head->next){
            len++;
            head = head->next;
        }
        //loop the link
        head->next = orighead;
        k = len - k%len-1; //find the front node of kth.
        while(k){
            newhead = newhead->next;
            k--;
        }
        ListNode* ret = newhead->next;
        newhead->next = NULL;
        return ret;
    }
};

 

83. Remove Duplicates from Sorted List:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *dup = NULL;
        ListNode *cur = head;
        if(cur == NULL || cur->next == NULL) return head;
        ListNode *nxt = cur->next;
        while(nxt != NULL){
            if(cur->val == nxt->val){
                ListNode *dup = nxt;
                nxt = nxt->next;
                cur->next = nxt;
                delete dup;
            }else{
                cur = nxt;
                nxt = nxt->next;
            }
        }
        return head;
    }
};

 

141. Linked List Cycle

/**
 * 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* slow = head;
        ListNode* fast = head;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next->next; //2step
            if(slow == fast) return true;
        }
        return false;
    }
};

   

147. Insertion Sort List

class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        ListNode* dummy = new ListNode(0);
        ListNode* pre = dummy;
        ListNode* curr = head;
        if(head == NULL || head->next == NULL) return head;
        ListNode* next = NULL;
        
        while(curr != NULL){
            next = curr->next;
            while(pre->next != NULL && pre->next->val < curr->val){ //find insert pos
                pre = pre->next;
            }
            curr->next = pre->next; //insert
            pre->next = curr;
            curr = next; //move curr to next node
            pre = dummy;//reset the pre
        }
        return dummy->next;
    }
};

 

148. Sort List

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        ListNode*p = head;
        ListNode*q = NULL;
        int temp = 0;
        if(head == NULL) return head;
        for(; p!=NULL; p=p->next)
            for(q=p->next; q!=NULL; q=q->next){
                if(p->val > q->val){
                    temp = p->val;
                    p->val = q->val;
                    q->val = temp;
                }
            }
        return head;
    }
};

 

160. Intersection of Two Linked Lists

/**
 * 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) {
        ListNode* p1 = headA;
        ListNode* p2 = headB;
        while(p1 != p2){
            p1 = p1?p1->next:headB;
            p2 = p2?p2->next:headA;
        }
        return p1;
    }
};

 

203. Remove Linked List Elements  

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

 

206. Reverse Linked List:

/**
 * 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) {
        if(head == NULL || head->next == NULL) return head;
        ListNode* pre = NULL;
        ListNode* cur = head;
        ListNode* nx = cur->next;
        while(nx != NULL){
            cur->next = pre;
            pre = cur;
            cur = nx;
            nx = nx->next;
        }
        cur->next = pre;
        return cur;
    }
};

 

237. Delete Node in a Linked List  

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode* p = node->next;
        node->val = p->val;
        node->next = p->next;
        delete p;
    }
};

 

https://www.cnblogs.com/upcwanghaibo/p/6928887.html

https://blog.csdn.net/qq_37466121/article/details/88204678

https://www.cnblogs.com/upcwanghaibo/p/6928887.html

  

  

posted on 2019-08-29 13:50  Felizño  阅读(211)  评论(0编辑  收藏  举报

导航