首页 > 其他分享 >day21

day21

时间:2022-11-18 21:25:57浏览次数:51  
标签:shortList return day21 next while longList curA

【面试题 02.07. 链表相交】

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int listLength(ListNode *head) { //返回链表结点总个数
        ListNode *temp = head;
        int result = 0;
        while (temp != nullptr) { 
            result++;
            temp = temp->next;
        }
        return result;
    }
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int ALength = listLength(headA);
        int BLength = listLength(headB); 
        ListNode *shortList = ALength < BLength ? headA : headB;
        ListNode *longList = ALength >= BLength ? headA : headB;
        if(shortList == nullptr || longList == nullptr)
            return nullptr;
        int extraNode = ALength >= BLength ? (ALength - BLength) : (BLength - ALength) ;
        while (extraNode) {
            longList = longList->next;
            extraNode--;
        }

        while (shortList != nullptr) {
            if (shortList == longList) {
                return shortList;
            }
            shortList = shortList->next;
            longList = longList->next;
        }
        return nullptr;
    }
};
  • 四十分钟好像,较慢,一开始没思路,双指针法中类似这种 fast先走 然后fast和slow一起走的题型我都想不到这个思路

  • 有了提示思路后 代码有几个问题

    • 关于while循环 以下两段代码等价 即while是先判断循环条件逻辑值 再执行循环体 后进行循环条件中的运算

      while (extraNode) {
                  longList = longList->next;
                  extraNode--;
              }
      while (extraNode--) {
                  longList = longList->next; 
              }
      
      
    • 此外关于while循环 注意与if循环的判断是有次数的去别的 前者在符合条件的情况下一直执行循环体 后者在符合条件的情况下只执行一次循环体

    • 主函数 最后return 是对边界特殊值等的处理语句 不能写成short->next 因为跳出循环时short==nullptr 那么之后就千万不能取值short->next 因为这等价于nullptr->next 这是不合法的 !!!》》》》》换句话说,当你return时 需要留意可以return空 但不能return 某个对空的操作 会报错!!!老是错!!!

    • 另外关于return 没有考虑当最开始的头指针为空的情况 也就是链表本身为空 就直接返回空值 不需要再操作了!!!老是忘!!!

  • 最总要的问题是 也是我花费时间最多的 是关于对未知谁是更大更长的两元素进行操作

    • 我的思路是: 找到谁长谁短并记下来:定义一个短链表指针 取比较语句中长度小的值 同理定义一个长链表指针 取比较语句中长度较长的值;定义一个表示两链表长度差值的整形变量 取比较语句中长度长的值-长度短的值的差;》》》》》最后遇到问题了 没考虑两链表长度相同的情况 所以在前面取值的时候 其中一个添加上等于号

      ListNode *shortList = ALength < BLength ? headA : headB;
      ListNode *longList = ALength >= BLength ? headA : headB;
      int extraNode = ALength >= BLength ? (ALength - BLength) : (BLength - ALength) ;
      
      	while (extraNode) {
                  longList = longList->next;
                  extraNode--;
      	}
      
      	while (shortList != nullptr) {
                  if (shortList == longList) {
                      return shortList;
                  }
                  shortList = shortList->next;
                  longList = longList->next;
      	}
      
    • 我之前的思路是:不管谁长谁短 两个while循环二选一移动指针就行

      while(countA -countB >0){
                  curA = curA->next;
                  countA--;
      }
      while(countB - countA >0){
                  curB = curB->next;
                  countB--;
      }
      while(curA != nullptr){
                  if(curA == curB)
                      return curA;
                  curA = curA->next;
                  curB = curB->next;
      }
      
    • 最好记得卡哥的思路:自己决定谁长谁短

      		// 让curA为最长链表的头,lenA为其长度
              if (lenB > lenA) {
                  swap (lenA, lenB);
                  swap (curA, curB);
              }
              // 求长度差
              int gap = lenA - lenB;
              // 让curA和curB在同一起点上(末尾位置对齐)
              while (gap--) {
                  curA = curA->next;
              }
              // 遍历curA 和 curB,遇到相同则直接返回
              while (curA != NULL) {
                  if (curA == curB) {
                      return curA;
                  }
                  curA = curA->next;
                  curB = curB->next;
              }
      
    • 这部分还需要 再看看消化消化
      明天继续:)

标签:shortList,return,day21,next,while,longList,curA
From: https://www.cnblogs.com/deservee/p/16904907.html

相关文章

  • 代码随想录Day21
    LeetCode111.给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树 [3,......
  • 牛客java选择题每日打卡Day21
    牛客java选择题每日打卡Day21......
  • day21 单列索引与组合索引 & 索引的优点和使用原则 & 视图与函数
    索引1.索引有几种四种,单列索引,组合索引,全文索引,空间索引2.索引的优点所有的MySQL数据库列类型都可以被索引,也就是可以给任意字段加索引提高数据查询速度索引的缺点1......
  • day21.内容回顾及总结
    前端体系结构及知识点一阶段(html+css)基础的页面布局(div弹性盒子布局)seo优化(搜索引擎优化)动画效果html5和css3多端适配(rem)二阶......
  • day21 线程终止与休眠
    线程停止让线程正常停止利用循环不能死循环使用标志位设置标志位不用stop函数和destroy1publicclassTestStopimplementsRunnable{2​3//1......
  • 代码随想录day21 | 530.二叉搜索树的最小绝对差 501. 二叉搜索树中的众数 236. 二叉
    530.二叉搜索树的最小绝对差题目|文章思路二叉搜索树的特点是按照中序遍历从小到大进行排列,因此,按照中序遍历,逐个比较即可找到最小差值进行中序遍历,当前节点和前一个......
  • 代码随想录day21
    530.二叉搜索树的最小绝对差解题步骤:1、将二叉搜索树转化为有序数组;2、按照重新排序后的数组进行遍历,获取最小的绝对值差。1classSolution{2private:3......
  • Day21
    List集合的排序sort()->对集合进行排序 Arrays.sort();注意1.如果集合中存放的是对象,如果想排序,就得实现以下步骤(1).在比较的对象类中实现Comparable接口(2).......
  • day21
    501.二叉搜索树中的众数classSolution{List<Integer>result=newArrayList<Integer>();intbase,count,maxCount;//用base来存当前的众数值,c......
  • 前端JS-Day21
    client系列:获得可视区域的相关信息clientWidth和offsetWidth区别:clientWidth只包含内容和padding,offsetWidth包含内容和内外边框。  立即执行函数:无需调用,直接执行......