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

链表中倒数第k个结点

时间:2016-09-19 19:14:56      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

输入一个链表,输出该链表中倒数第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

 

链表中倒数第k个结点

标签:

原文地址:http://www.cnblogs.com/yuguangyuan/p/5885989.html

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