zoukankan      html  css  js  c++  java
  • 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List

      The task is reversing a list in range m to n(92) or a whole list(206).

      All in one : U need three pointers to achieve this goal. 

       1) Pointer to last value

       2) Pointer to cur p value

       3) Pointer to next value

      Here, showing my code wishes can help u.

      Of course, you also need to record four nodes in special postions. 

       1) newM  2)newN  3)beforeM  4)afterN

      These may be some complex(stupid) but it's really friend to people who are reading my code and easily understood.

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    typedef struct ListNode {
        int val;
        ListNode *next;
        ListNode(int x) : val(x), next(NULL) {}
    }ListNode, *PNode;
    
    void create_List(PNode head)
    {
        PNode  p = head;
        int n;
        cin>>n;
        for(int i = 0; i < n ;i ++){
            int t;
            cin>>t;
            if(i == 0){
                head -> val = t;
                head -> next = NULL;
                cout<<"head is "<<head->val<<endl;
                p = head;
            }else{
                PNode newNode = (PNode) malloc(sizeof(PNode));
                newNode -> val = t;
                newNode -> next = NULL;
                p -> next = newNode;
                p = newNode;
                cout<<"p is "<<p -> val<<endl;
            }
        }
    }
    
    void display(PNode  head)
    {
        PNode p = head;
        while(p){
            cout<<p->val<<" -> ";
            p = p -> next;
        }cout<<endl;
    }
    
    class Solution {
    public:
        ListNode* reverseBetween(ListNode* head, int m, int n) {
            if(m == n || head == NULL) return head;
            ListNode *pLast = head, *p = head -> next, *pNext = NULL;
            ListNode *newN = NULL, *newM = NULL, *beforeM = head, *afterN = NULL;
            int pos = 1;
            while(p){
                if(pos == m - 1){
                    beforeM = pLast;
                    pLast = p;
                    p = p -> next;
                }
                else if(pos >= m && pos < n){
                    pNext = p -> next;
                    p -> next = pLast;
                    if(pos == m){
                        pLast -> next = NULL;
                        newM = pLast;
                    }
                    pLast = p;
                    if(pos == n - 1){
                        newN = p;
                        afterN = pNext;
                    }
                    p = pNext;
                }else{
                    pLast = p;
                    p = p -> next;
                }
                pos ++;
            }
            if( m==1 && afterN == NULL){
                head = newN;
            }else if(m == 1){
                head = newN;
                newM -> next = afterN;
            }else{
                beforeM -> next = newN;
                newM -> next = afterN;
            }
            return head;
        }
    
        ListNode* reverseList(ListNode* head) {
            if(head == NULL) return head;
            ListNode *pLast = head, *p = head -> next, *pNext = NULL;
            while(p){
                pNext = p -> next;
                p -> next = pLast;
                if(pLast == head){
                    pLast -> next = NULL;
                }
                pLast = p;
                p = pNext;
            }
            head = pLast;
            return head;
        }
    };
    int main()
    {
        PNode head = (PNode) malloc(sizeof(PNode));;
        create_List(head);
        cout<<"after creating , head is "<<head->val<<endl;
        display(head);
        Solution tmp = Solution();
        //tmp.reverseBetween(head, 2, 3);
        tmp.reverseList(head);
        system("pause");
        return 0;
    }
  • 相关阅读:
    python+selenium自动化软件测试(第7章):Page Object模式
    python+selenium自动化软件测试(第6章):selenium phantomjs页面解析使用
    python+selenium自动化软件测试(第5章):Selenium Gird
    python+selenium自动化软件测试(第3章):unittest
    python+selenium自动化软件测试(第2章):WebDriver API
    python+selenium自动化软件测试(第1章):环境搭建,你也可以直接用Anaconda!
    js 躲避球游戏
    ES6基础教程,常见特性随笔
    JS 回到顶端 back to top
    单Js 的重力游戏开发
  • 原文地址:https://www.cnblogs.com/luntai/p/5624169.html
Copyright © 2011-2022 走看看