zoukankan      html  css  js  c++  java
  • 链表中倒数第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
  • 相关阅读:
    streamreader
    python编码问题总结
    linux rz上传文件及出错解决方案
    使用linux远程登录另一台linux
    利用Django实现RESTful API(一)
    windows下搭建vue开发环境
    windows下python虚拟环境virtualenv安装和使用
    SonarQube的安装、配置与使用
    windows下安装Mysql(图文详解)
    静态代码分析工具sonarqube+sonar-runner的安装配置及使用
  • 原文地址:https://www.cnblogs.com/yuguangyuan/p/5885989.html
Copyright © 2011-2022 走看看