• LeetCode 206. Reverse Linked List


    https://leetcode.com/problems/reverse-linked-list/description/

    Reverse a singly linked list.

    Hint:

    A linked list can be reversed either iteratively or recursively. Could you implement both?


    • 链表题。思路要清晰,别弄混了。
    • 迭代版。三个指针,previous, current, next。整体思路是从头往尾处理,把前一个指针赋给当前指针的next之后,把三个指针再右移,继续处理下次循环,直到当前指针为nullptr。首先保存pCur->next到pNext,然后反转previous和current,pCur->next = pPre。之后把previous和current指针往右移继续处理下一个循环,pPre = pCur, pCur = pNext。注意循环结束的判断条件是pCur != nullptr。因为最后一次循环到尾指针,则会把尾指针的next指针null赋值给pCur,pCur赋值给pPre,循环正好结束。此时pCur则为反转之后的头指针,因为前面的指针都已经在它的next里了。
    • 递归版有两个。一个需要再借助一个函数,按照非递归版重写的;另一个比较tricky不需要额外函数。
    • Reverse Linked List - LeetCode
      • https://leetcode.com/problems/reverse-linked-list/solution/
      1 //
      2 //  main.cpp
      3 //  LeetCode
      4 //
      5 //  Created by Hao on 2017/3/16.
      6 //  Copyright © 2017年 Hao. All rights reserved.
      7 //
      8 
      9 #include <iostream>
     10 #include <vector>
     11 #include <queue>
     12 #include <set>
     13 using namespace std;
     14 
     15 /**
     16  * Definition for singly-linked list.
     17  */
     18 struct ListNode {
     19     int val;
     20     ListNode *next;
     21     ListNode(int x) : val(x), next(NULL) {}
     22 };
     23 
     24 class Solution {
     25 public:
     26     // Iterative solution
     27     ListNode* reverseList(ListNode* head) {
     28         // corner case
     29         if (head == nullptr || head->next == nullptr)
     30             return head;
     31         
     32         ListNode *pPre = nullptr, *pCur = head;
     33         
     34         while (pCur != nullptr) {
     35             ListNode* pNext = pCur->next; // store the next pointer
     36             pCur->next = pPre; // reverse the previous and current pointer
     37             
     38             // move the previous and current pointer rightward for the next loop
     39             pPre = pCur;
     40             pCur = pNext;
     41         }
     42         
     43         return pPre;
     44     }
     45     
     46     // Recursive solution
     47     ListNode* reverseList2(ListNode* head) {
     48         ListNode *pPrevious = nullptr, *pCurrent = head;
     49         
     50         return reverse(pPrevious, pCurrent);
     51     }
     52     
     53     ListNode* reverse(ListNode* pPre, ListNode* pCur) {
     54         if (pCur == nullptr) return pPre;
     55         
     56         ListNode* pNext = pCur->next;
     57         pCur->next = pPre;
     58         
     59         return reverse(pCur, pNext);
     60     }
     61     
     62     // Tricky Recursive solution
     63     ListNode* reverseList3(ListNode* head) {
     64         if (head == nullptr || head->next == nullptr) return head;
     65         
     66         ListNode* p = reverseList3(head->next);
     67         
     68         head->next->next = head;
     69         head->next = nullptr;
     70         
     71         return p;
     72     }
     73     
     74     ListNode* createList() {
     75         ListNode    *pCur = nullptr, *pHead = nullptr;
     76         
     77         for (int i = 0; i < 10; i ++) {
     78             ListNode* pTemp = new ListNode(i);
     79             
     80             if (0 == i) {
     81                 pHead = pCur = pTemp;
     82             } else {
     83                 pCur->next = pTemp;
     84                 pCur = pCur->next;
     85             }
     86         }
     87         
     88         return pHead;
     89     }
     90     
     91     void printList(ListNode* head) {
     92         if (head != nullptr) {
     93             cout << head->val << endl;
     94             printList(head->next);
     95         } else {
     96             cout << endl;
     97         }
     98     }
     99     
    100     void deleteList(ListNode** head) {
    101         ListNode* pDel = *head;
    102         
    103         while (pDel != nullptr) {
    104             ListNode* pNext = pDel->next;
    105             
    106             delete pDel;
    107             pDel = nullptr;
    108             
    109             pDel = pNext;
    110         }
    111 
    112         // neccessary?
    113         *head = nullptr;
    114     }
    115 };
    116 
    117 int main(int argc, char* argv[])
    118 {
    119     Solution    testSolution;
    120     
    121     /*
    122      0
    123      1
    124      2
    125      3
    126      4
    127      5
    128      6
    129      7
    130      8
    131      9
    132      
    133      9
    134      8
    135      7
    136      6
    137      5
    138      4
    139      3
    140      2
    141      1
    142      0
    143      */
    144     // call reverseList()
    145     ListNode* pHead = testSolution.createList();
    146     
    147     testSolution.printList(pHead);
    148     
    149     ListNode* pRHead = testSolution.reverseList(pHead);
    150     
    151     testSolution.printList(pRHead);
    152 
    153     testSolution.deleteList(&pRHead);
    154 
    155     // call reverseList2()
    156     pHead = testSolution.createList();
    157     
    158     testSolution.printList(pHead);
    159     
    160     pRHead = testSolution.reverseList2(pHead);
    161     
    162     testSolution.printList(pRHead);
    163     
    164     testSolution.deleteList(&pRHead);
    165 
    166     // call reverseList3()
    167     pHead = testSolution.createList();
    168     
    169     testSolution.printList(pHead);
    170     
    171     pRHead = testSolution.reverseList3(pHead);
    172     
    173     testSolution.printList(pRHead);
    174     
    175     testSolution.deleteList(&pRHead);
    176 
    177     return 0;
    178 }
    View Code
  • 相关阅读:
    linux性能指令分析进阶篇
    数据库提升篇
    linux之基础命令大全
    数据库事务测试以及级联更新级联删除
    【Comet OJ】—模拟赛测试 Day1题解
    【Comet OJ】—模拟赛测试 Day1题解
    【LOJ # 6268】—分拆数(生成函数+多项式Ln/Exp+NTT)
    【LOJ # 6268】—分拆数(生成函数+多项式Ln/Exp+NTT)
    【LOJ #6041】【雅礼集训 2017 Day7】—事情的相似度(后缀自动机+LCT+树状数组)
    【LOJ #6041】【雅礼集训 2017 Day7】—事情的相似度(后缀自动机+LCT+树状数组)
  • 原文地址:https://www.cnblogs.com/pegasus923/p/8448702.html
Copyright © 2020-2023  润新知