ReverseLinkedListII-創(chuàng)新互聯(lián)

描述

10年積累的成都做網(wǎng)站、網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有囊謙免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

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->nullptr, m = 2 and n = 4,

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

Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.

這是第一次實現(xiàn)的代碼(很挫—_—)

typedef struct ListNode
{
	int _var;
	struct ListNode *_next;

	ListNode(int var)
		:_var(var)
		, _next(NULL)
	{}
}node,*node_p;
class Solution
{
public:
	node_p ReserveList(node_p &head,int m,int n)
	{
		//檢查邊界條件
		if (head == NULL){
			printf("List is NULL\n");
			return NULL;
		}
		if (m<1||n<m){//未檢查n的邊界
			printf("rangle is error\n");
			return NULL;
		}
		if (n == m)
			return head;
		//******************
		node_p prev = head;
		node_p a = head;
		node_p b = head;
		for (int i = 2; i < m; ++i){
			prev = prev->_next;
		}
		for (int i = 1; i < m; ++i){
			a = a->_next;
		}
		for (int i = 1; i < n; ++i){
			b = b->_next;
		}

		node_p tmp = new node(-1);
		//a->_next = b->_next;
		node_p last = a;
		while (a != b){
			if (m == 1)
				prev = prev->_next;
			else
				prev->_next = a->_next;
			a->_next = tmp->_next;
			tmp->_next = a;
			if (m == 1)
				a = prev;
			else
				a = prev->_next;
		}
		if (m == 1){
			prev = b->_next;
			b->_next = tmp->_next;
			tmp->_next = b;
			last->_next = prev;
			node_p Newhead = tmp->_next;
			free(tmp);
			return Newhead;
		}
		prev->_next = b->_next;
		b->_next = tmp->_next;
		tmp->_next = b;
		last->_next = prev->_next;
		prev->_next = tmp->_next;
		free(tmp);
		return head;
	}

};

這是重新寫的代碼(還是很挫,感覺整個人都不好了)

reverse_linklist.h:

#pragma once
#include <iostream>                                                             
#include <assert.h>
#include <stdlib.h>
 
using namespace std;
 
typedef struct ListNode
{
    int _var;
    ListNode *_next;
 
    ListNode(int var)
        :_var(var)
         ,_next(NULL)
    {}  
}node,*node_p;

class Solution
{                                                                               
public:
    node_p reverse_link(node_p &list,int m,int n)
    {
            //邊界檢查
        if(list==NULL)
            return NULL;
        if(m<1||m>n){
            cout<<"parameter error"<<endl;
            return NULL;
        }
        if(m==n)
            return list;
        node dummy(-1);
        node_p head=&dummy;
        head->_next=list;
        for(int i=0;i<m-1;++i){
            head=head->_next;
        }
        node_p first=list;
        for(int i=1;i<m;++i)
            first=first->_next;
        node_p second=first;
        for(int i=m;i<n;++i)
            second=second->_next;
        node_p tmp=first;
        
        //核心步驟
        while(tmp!=second){
            tmp=first->_next;
            first->_next=tmp->_next;                                            
            tmp->_next=head->_next;
            head->_next=tmp;
        }
        
        if(m==1)
            return head->_next;
        return list;
    }
};

test.cpp

#include "reverse_linklist.h"
        
using namespace std;
                                                                                
int main()
{       
    node_p n1 = new node(1);
    node_p n2 = new node(2);
    node_p n3 = new node(3);
    node_p n4 = new node(4);
    node_p n5 = new node(5);
    n1->_next = n2;
    n2->_next = n3;
    n3->_next = n4;
    n4->_next = n5;
    Solution s;
    node_p newhead=s.reverse_link(n1,3,5);
    while (newhead != NULL){
        node_p tmp = newhead;
        cout<<tmp->_var<<"  ";
        newhead = newhead->_next;
        free(tmp);
    }   
     cout<<endl;
     return 0;
}

運行結(jié)果:

Reverse Linked List II

還是來看看人家的代碼吧:

Reverse Linked List II

自己還是弱的很,需要更努力啦^_^

《完》

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章標題:ReverseLinkedListII-創(chuàng)新互聯(lián)
標題來源:http://bm7419.com/article10/dgopgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google建站公司、網(wǎng)站營銷、App設(shè)計網(wǎng)站內(nèi)鏈、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化