链表中倒数第k个结点

输入一个链表,输出该链表中倒数第k个结点。

思路:使用中间变量vector去接node

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
    
        ListNode*head = pListHead;
        
        vector<ListNode*>node;
        
        if(k<=0){
            return NULL;
        }
        while(head!=NULL){
            node.push_back(head);
            head=head->next;
        }
        if(k>node.size()){
            return NULL;
        }
        return node[node.size()-k];
 
    }
};
View Code

思路:先求总长,总长减去倒数k,len-k,就是正着数的位置

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
    
        ListNode*head = pListHead;
        ListNode*nodeK=pListHead;
        int len=0;//链表总长
        int index=0;//
        
        if(k<=0){
            return NULL;
        }
        while(head!=NULL){
              len++;
            head=head->next;
        }
        if(k>len){
            return NULL;
        }
        index = len-k;
        
        while(index--){
            nodeK = nodeK->next;
        }
        return nodeK;
        
 
    }
};
View Code

 

合并两个排序的链表

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
        /*
  {
        ListNode *head = NULL, *node = NULL;
        while (pHead1 || pHead2){
            if (((pHead1 == NULL) && (pHead2 != NULL)) || pHead1->val >= pHead2->val){
                if (head){
                    node->next = pHead2;
                    node = pHead2;
                }
                else{
                    node = pHead2;
                    head = pHead2;
                }
                pHead2 = pHead2->next;
            }
            else if (((pHead2 == NULL) && (pHead1 != NULL)) || pHead1->val <= pHead2->val){
                if (head){
                    node->next = pHead1;
                    node = pHead1;
                }
                else{
                    node = pHead1;
                    head = pHead1;
                }
                pHead1 = pHead1->next;
            }
        }
         node->next=NULL;
         return head;
    }
    */
            {
        ListNode *head = NULL, *node = NULL;
        if (pHead1 == NULL)
            return pHead2;
        if (pHead2 == NULL)
            return pHead1;
        while (pHead1 && pHead2){
            if ( pHead1->val >= pHead2->val){
                if (head){
                    node->next = pHead2;
                    node = pHead2;
                }
                else{
                    node = pHead2;
                    head = pHead2;
                }
                pHead2 = pHead2->next;
            }
            else if ( pHead1->val <= pHead2->val){
                if (head){
                    node->next = pHead1;
                    node = pHead1;
                }
                else{
                    node = pHead1;
                    head = pHead1;
                }
                pHead1 = pHead1->next;
            }
        }
        if (pHead1 == NULL){
            node->next = pHead2;
        }
        if (pHead2 == NULL){
            node->next = pHead1;
        }
         return head;
    }
};
View Code

 反转链表

class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {

        ListNode* h = NULL;
        ListNode* p = pHead;
        while(p){
            ListNode* tmp = p -> next;
            p -> next = h;
            h = p;
            p = tmp;
        }
        return h;
    }
   
};
View Code

 

posted @ 2016-09-19 17:32  于光远  阅读(150)  评论(0编辑  收藏  举报