码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode-Linked List

时间:2015-12-14 21:29:35      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

Linked List

24 Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* swapPairs(struct ListNode* head) {
    //空链表和单一节点链表,不作处理
    if(NULL == head || NULL == head->next) return head;
    struct ListNode *p1, *p2, *pPre, *pNext;
    p1 = head, p2 = head->next;
    //第一对节点,做特殊处理
    pNext = p2->next;
    p2->next = p1;
    p1->next = pNext;
    pPre = p1;
    head = p2;  //这一句是特殊处理的理由
    while(pNext)
    {
        p1 = pNext;
        p2 = p1->next;
        if(NULL == p2) break;
        pNext = p2->next;
        //以下两句代码:逆置相邻节点
        p2->next = p1;
        p1->next = pNext;
        pPre->next = p2;
        pPre = p1;
    }
    return head;
}

148 Sort List

Sort a linked list in O(n log n) time using constant space complexity.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
#include <algorithm>
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if(NULL == head || NULL == head->next){
            return head;
        }
        vector<int> vec;
        ListNode *p = head;
        while(p){
            vec.push_back(p->val);
            p = p->next;
        }
        sort(vec.begin(), vec.end());   //n*logn
        p = head;
        int i = 0;
        while(p){
            p->val = vec[i];
            i++;
            p = p->next;
        }
        return head;
    }
};

61 Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* rotateRight(struct ListNode* head, int k) {
    if(NULL == head || NULL == head->next || 0 == k) return head;
    struct ListNode *p, *pEnd;
    p = pEnd = head;
    int n = 1;
    while(pEnd->next)
    {
        pEnd = pEnd->next;
        n++;
    }
    k %= n;  //规范k值
    k = n - k;
    k--;
    while(k)
    {
        p = p->next;
        k--;
    }
    pEnd->next = head;
    head = p->next;
    p->next = NULL;
    return head;
}

25 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

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseKGroup(struct ListNode* head, int k) {
    if(NULL == head || NULL == head->next || k < 2) return head;
    int i, n;
    n = 0;
    struct ListNode *p = head;
    while(p){
        n++;
        p = p->next;
    }
    struct ListNode *pPre, *pCur, *pNext;
    struct ListNode *pStart, *pEnd;
    pStart = head;
    if(n >= k){
        pPre = pStart, pCur = pStart->next;
        i = 1;
        while(i < k){
            pNext = pCur->next;
            pCur->next = pPre;
            pPre = pCur;
            pCur = pNext;
            i++;
        }
        pEnd = pStart;
        pEnd->next = pCur;
        head = pPre;
        n -= k;
    }
    while(n >= k){
        pStart = pCur;
        pPre = pStart, pCur = pStart->next;
        i = 1;
        while(i < k){
            pNext = pCur->next;
            pCur->next = pPre;
            pPre = pCur;
            pCur = pNext;                
            i++;
        }
        pEnd->next = pPre;
        pStart->next = pCur;
        pEnd = pStart;
        n -= k;
    }
    return head;
}

206 Reverse Linked List

Reverse a singly linked list.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head) {
    if (NULL == head || NULL == head->next) return head;
    struct ListNode *pPre, *pCur, *pNext;
    pPre = head, pCur = head->next;
    while (pCur)
    {
        pNext = pCur->next;
        if (pPre == head) pPre->next = NULL;
        pCur->next = pPre;
        pPre = pCur;
        pCur = pNext;
    }
    head = pPre;
    return head;    
}

92 Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,

return 1->4->3->2->5->NULL.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
    if(NULL == head || NULL == head->next || m <= 0 || m >= n){
        return head;
    }
    int len = 0;
    struct ListNode *p = head;
    while(p){
        len++;
        p = p->next;
    }
    if(n > len){
        return head;
    }
    struct ListNode *pPre, *pStart, *pCur, *pNext;
    int i = 1;
    pPre = NULL, pStart = head;
    while(i < m){
        i++;
        pPre = pStart;
        pStart = pStart->next;
    }
    p = pStart;
    pCur = p->next;
    while(i < n){
        pNext = pCur->next;
        pCur->next = p;
        p = pCur;
        pCur = pNext;
        i++;
    }
    if(NULL == pPre){
        head = p;
    }
    else{
        pPre->next = p;
    }
    pStart->next = pCur;
    return head;
}

143 Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…

You must do this in-place without altering the nodes‘ values.

For example,
Given {1,2,3,4}, reorder it to {1,4,2,3}.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void reorderList(struct ListNode* head) {
    //空链表、单个节点或双节点都不用处理
    if (NULL == head || NULL == head->next || NULL == head->next->next) return;
    //寻找中间节点
    struct ListNode *fast, *slow;
    struct ListNode *pCur, *pPre, *pNext;
    struct ListNode *p1, *p2, *p;
    int flag1, flag2, n;
    flag1 = 2, n = 1;
    fast = slow = head;
    while (fast)
    {
        fast = fast->next;
        if (fast)
        {
            fast = fast->next;
            if (NULL == fast)
            {
                flag2 = 2;   //偶数个节点
                break;
            }
        }
        else
        {
            flag2 = 1;   //奇数个节点
            break;
        }
        slow = slow->next;
        n++;
    }
    if (slow->next->next)
    {
        pPre = slow->next;
        pCur = pPre->next;
        while (pCur)
        {
            pNext = pCur->next;
            pCur->next = pPre;    //逆置
            if (pPre == slow->next) pPre->next = NULL;    //置空,作为新的链表结尾
            pPre = pCur;
            pCur = pNext;
        }
        slow->next = pPre;    //连接
    }
    //将前后两部分合并
    p = head, p1 = head->next, p2 = slow->next;
    while (n)
    {
        if ((flag2 == 1 && p == slow) || (flag2 == 2 && p->next == NULL)) break;
        if (flag1 == 1)
        {
            p->next = p1;
            p = p1;
            if (p1) p1 = p1->next;
            flag1 = 2;
        }
        else
        {
            p->next = p2;
            p = p2;
            if (p2) p2 = p2->next;
            flag1 = 1;
        }
    }
    p->next = NULL;
}

19 Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
    if(NULL == head || n <= 0){
        return head;
    }
    int len = 0;
    ListNode *p = head;
    while(p){
        len++;
        p = p->next;
    }
    if(n >= len){
        n = len;
    }
    if(n == len){
        head = head->next;
    }
    else{
        int i = 1;
        p = head;
        while(i < len - n){
            i++;
            p = p->next;
        }
        p->next = p->next->next;
    }
    return head;    
}

203 Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* removeElements(struct ListNode* head, int val) {
    ListNode *p, *q;
    while(head && head->val == val){
        p = head;
        head = head->next;
        //delete p;
    }
    if(NULL == head || NULL == head->next){
        return head;
    }
    p = head;
    while(p){
        q = p->next;
        while(q && q->val == val){
        q = q->next;
        }
        p->next = q;
        p = q;
    }
    return head;    
}

83 Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if(NULL == head || NULL == head->next){
        return head;
    }
    ListNode *p1, *p2;
    p1 = head;
    while(1){
        p2 = p1->next;
        while(p2 && p2->val == p1->val){
            p2 = p2->next;
        }
        p1->next = p2;
        p1 = p2;
        if(p2 == NULL){
            break;
        }
    }
    return head;    
}

82 Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
#include <map>
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(NULL == head || NULL == head->next){
            return head;
        }
        map<int, int> m;
        ListNode *pPre, *pCur;
        pCur = head;
        while(pCur){
            auto ite = m.find(pCur->val);
            if(ite != m.end()){
                (ite->second)++;
            }
            else{
                m.insert(make_pair(pCur->val, 1));
            }
            pCur = pCur->next;
        }
        pPre = NULL, pCur = head;
        auto ite = m.begin();
        while(ite != m.end()){
            if(ite->second == 1){
                pCur->val = ite->first;
                pPre = pCur;
                pCur = pCur->next;
            }
            ++ite;
        }
        if(pPre == NULL){
            head = NULL;
        }
        else{
            pPre->next = NULL;
        }
        return head;
    }
};

86 Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
#include <vector>
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        if(NULL == head) return head;
        vector<int> vec;
        ListNode *p = head;
        while(p)
        {
            vec.push_back(p->val);
            p = p->next;
        }
        //先把小于x的数放进去
        p = head;
        for(int i = 0; i < vec.size(); i++)
        {
            if(vec[i] < x)
            {
                p->val = vec[i];
                p = p->next;
            }
        }
        //最后把不小于x的数放进去
        for(int i = 0; i < vec.size(); i++)
        {
            if(vec[i] >= x)
            {
                p->val = vec[i];
                p = p->next;
            }
        }        
        return head;
    }
};

234 Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
 #include <vector>
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if(NULL == head || NULL == head->next){
            return true;
        }
        vector<int> vec;
        ListNode *p = head;
        while(p){
            vec.push_back(p->val);
            p = p->next;
        }
        int size = vec.size();
        for(int i = 0; i <= size / 2; i++){
            if(vec[i] != vec[size - 1 - i]){
                return false;
            }
        }
        return true;
    }
};

21 Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    if(NULL == l1){
        return l2;
    }
    if(NULL == l2){
        return l1;
    }
    struct ListNode *head, *p1, *p2, *p;
    int flag = 0;
    p1 = l1, p2 = l2;
    if(p1->val < p2->val){
        head = p1;
        p1 = p1->next;
        flag = 1;
    }
    else{
        head = p2;
        p2 = p2->next;
        flag = 2;
    }
    p = head;
    while(p1 && p2){
        if(flag == 1){
            while(p1 && (p1->val <= p2->val)){
                p1 = p1->next;
                p = p->next;
            }
            if(!p1 || !p2){
                break;
            }
            p->next = p2;
            p2 = p2->next;
            p = p->next;
            flag = 2;
        }
        else{
            while(p2 && (p2->val <= p1->val)){
                p2 = p2->next;
                p = p->next;
            }
            if(!p1 || !p2){
                break;
            }
            p->next = p1;
            p1 = p1->next;
            p = p->next;
            flag = 1;            
        }
    }
    if(p1){
        p->next = p1;
    }
    if(p2){
        p->next = p2;
    }
    return head;    
}

23 Merge k Sorted Lists

141 Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    if (NULL == head || NULL == head->next) {
        return false;
    }
    struct ListNode *p1, *p2;
    p1 = head, p2 = head->next;
    while (true) {
        if (NULL == p2) {
            false;
        }
        if (p1 == p2) {
            return true;
        }
        p1 = p1->next;
        if (NULL == p2->next) {
            return false;
        }
        else {
            p2 = p2->next;
            if (NULL == p2->next) {
                return false;
            }
            p2 = p2->next;
        }
    }    
}

142 Linked List Cycle II

160 Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.


For example, the following two linked lists:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3
begin to intersect at node c1.

Notes:
If the two linked lists have no intersection at all, return null.
The linked lists must retain their original structure after the function returns.
You may assume there are no cycles anywhere in the entire linked structure.
Your code should preferably run in O(n) time and use only O(1) memory.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if(NULL == headA || NULL == headB){
        return NULL;
    }
    int i, n1, n2;
    n1 = n2 = 1;
    struct ListNode *p1, *p2;
    p1 = headA, p2 = headB;
    while(p1->next){
        n1++;
        p1 = p1->next;
    }
    while(p2->next){
        n2++;
        p2 = p2->next;
    }
    if(p1 != p2){
        return NULL;
    }
    p1 = headA, p2 = headB;
    if(n1 >= n2){
        for(i = 0; i < n1 - n2; i++){
            p1 = p1->next;
        }
    }
    else{
        for(i = 0; i < n2 - n1; i++){
            p2 = p2->next;
        }        
    }
    while(p1 != p2){
        p1 = p1->next;
        p2 = p2->next;
    }
    return p1;
}

147 Insertion Sort List

Sort a linked list using insertion sort.

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
void swap(int *a, int *b){
    int t = *a;
    *a = *b;
    *b = t;
}
struct ListNode* insertionSortList(struct ListNode* head) {
    if(NULL == head || NULL == head->next){
        return head;
    }
    int i, j, n = 0;
    struct ListNode *p = head;
    while(p){
        n++;
        p = p->next;
    }
    int *nums = (int*)malloc(sizeof(int)*n);
    p = head;
    i = 0;
    while(p){
        nums[i++] = p->val;
        p = p->next;
    }
    //insertion sort
    for(i = 1; i < n; i++){
        j = i;
        while(j && nums[j] < nums[j - 1]){
            swap(nums + j, nums + j - 1);
            j--;
        }
    }
    p = head;
    i = 0;
    while(p){
        p->val = nums[i];
        p = p->next;
        i++;
    }
    free(nums);
    return head;
}

237 Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

参考:

/**
 * 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) {
        if(node){
            ListNode *p1, *p2;
            p1 = node, p2 = node->next;
            while(p2->next){
                p1->val = p2->val;
                p1 = p2;
                p2 = p2->next;
            }
            p1->val = p2->val;
            delete p2;
            p1->next = NULL;
        }
    }
};

138 Copy List with Random Pointer

109 Convert Sorted List to Binary Search Tree

2 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

参考:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    if(NULL == l1) return l2;
    if(NULL == l2) return l1;
    struct ListNode *head = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode *p1, *p2, *p;
    p1 = l1, p2 = l2, p = head;
    int more = 0;  //more表示进位值
    p->val = (p1->val + p2->val) % 10;
    more = (p1->val + p2->val) / 10;
    p1 = p1->next, p2 = p2->next;
    while(p1 && p2)
    {
        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        p = p->next;
        p->val = (p1->val + p2->val + more) % 10;
        more = (p1->val + p2->val + more) / 10;    
        p1 = p1->next, p2 = p2->next;
    }
    while(p1)
    {
        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        p = p->next;
        p->val = (p1->val + more) % 10;
        more = (p1->val + more) / 10;
        p1 = p1->next;
    }
    while(p2)
    {
        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        p = p->next;
        p->val = (p2->val + more) % 10;
        more = (p2->val + more) / 10;
        p2 = p2->next;        
    }
    if(more)//仍有进位
    {
        p->next = (struct ListNode*)malloc(sizeof(struct ListNode));
        p = p->next;
        p->val = more;
    }
    p->next = NULL;
    return head;
}

所有代码下载:LeetCode-Linked List

期待更好的解法~

LeetCode-Linked List

标签:

原文地址:http://blog.csdn.net/zhangxiangdavaid/article/details/50295727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!