链表

  1. 不给链表头,删除某个节点?或在O(1)时间删除一个结点?leetcode
    1 void deleteNode(ListNode* node) {
    2     *node = *node->next;
    3 }
    4  有leakmemory ,5 void deleteNode(ListNode* node) {
    6     auto next = node->next;
    7     *node = *next;
    8     delete next;
    9 }

    但node=node->next是错误的:

    former=node这样的赋值操作可能不存在,node=node->next是node指向了下一个结点,而former不等于node。

  2. 循环里递增字符指针两次导致的越界错误
     1 int findMatch(const char* number0, const char* number1){
     2     
     3     char *p = const_cast<char*> (number0);
     4     char *q = const_cast<char*>(number1);
     5     int count = 0;
     6     int max = count;
     7     while ((*p != '\0') && (*q != '\0'))
     8     {
     9         while ((*p==*q)&&*p!='\0')
    10         {
    11             count++;
    12             p++; q++;   
    15         }
    16         //if (*p != '\0'){ p++; q++; }
    17         if (max < count) max = count;
    18         count = 0;
    19     }
    20     if (max == 1 || max == 0) return 0;
    21     return max;
    23 }

    第16行的代码如果不检测越界,直接 p++,上一个while结束时可能p='\0'了,这样p指向了字符串结尾的后面,最外层循环就检测不到终止条件了。最外层循环只能针对p++一次的情况,不能针对两次检测

  3. 单链表逆序
posted @ 2017-08-17 01:04  hchacha  阅读(163)  评论(0)    收藏  举报