【作业6】链表专题:链表的第一个中间结点

对应课程链接:拿捏算法面试:链表的中间结点

作业要求:

在 链表的中间结点 这道题中,如果节点个数是偶数,我们求的是第二个中间节点,现在我们做一下改变,要求返回第一个中间节点。
1、为了统一格式,为下面定义了一个方法

ListNode middleFirstNode(ListNode head){

}

请打卡给出你的代码。

答案:Leetcode 题解区就可以看到了,第一个精选题解就有。

PS:如果你不知道怎么打卡,可以看这里:打卡细则

共以下 28 个回答

  • 幽小皮 永久会员 2024年4月1日 下午7:37

    打卡

    class Solution {
        public ListNode middleNode(ListNode head) {
            if(head == null){
                return null;
            }else if(head.next == null){
                return head;
            }
            ListNode slow = head, fast = head;
            while(fast.next != null && fast.next.next != null){
                slow = slow.next;
                fast = fast.next.next;
            }
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • 周四 永久会员 2024年3月8日 上午10:11

    打卡

    class Solution {
        public ListNode middleFisetNode(ListNode head) {
            if(head == null ||head.next == null){
                return head;
            }
            ListNode slow =  head;
            ListNode fast =  head;
    
            //while(fast != null && fast.next != null){
            while(fast.next != null && fast.next.next != null)
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • 塔底的仰望 永久会员 2023年10月14日 下午9:36

    打卡

    public ListNode middleNode(ListNode head) {
            if (head == null || head.next == null) {
                return null;
            }
    
            ListNode slow = head;
            ListNode fast = head;
            while (fast.next != null &&  fast.next.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
    
            return slow;
        }
    
    0 赞同 0 条回复
  • 星河揽我入梦 永久会员 2023年10月2日 上午11:22

    ok

    0 赞同 0 条回复
  • 泡芙 永久会员 2023年9月5日 下午10:29
    class Solution {
        public ListNode middleFirstNode(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
    
            ListNode fast = head;
            ListNode slow = head;
    
            // 关键点
            while (fast.next != null && fast.next.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;        
        }
    }
    
    0 赞同 0 条回复
  • Thaddeus 永久会员 2023年8月29日 下午2:35
    打卡
    ListNode* middleFirstNode(ListNode* head){
        if(head == nullptr || head->next == nullptr) return head;
    
        ListNode* slow = head;
        ListNode* fast = head;
        while(fast->next != nullptr && fast->next->next != nullptr) {
            fast = fast->next->next;
            slow = slow->next;
        }
        return slow;
    }
    
    0 赞同 0 条回复
  • 阿尚 普通 2023年8月17日 上午10:44
    //链表的第一个中心节点
    class Solution {
        public ListNode middleNode(ListNode head) {
            if(head == null) return null;
    
            ListNode slow = head;
            ListNode fast = head;
    
            while(fast.next != null && fast.next.next != null){
                slow = slow.next;
                fast = fast.next.next;
            }
    
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • 纸飞机 永久会员 2023年8月8日 下午4:54
    //改成do_while
    class Solution {
    public:
        ListNode* middleNode(ListNode* head) {
            if(!head)
            {
                return nullptr;
            }
            ListNode*slow = head;
            ListNode*fast = head;
            do
            {
                slow = slow->next;
                if(fast->next->next)
                {
                    fast = fast->next->next;
                }
                else
                {
                    fast = fast->next;
                }
    
    
            }while(fast->next && fast->next->next);
            return slow;
        }
    };
    
    0 赞同 0 条回复
  • 𬍤 永久会员 2023年7月28日 下午12:10
    class Solution {
        public ListNode middleFirstNode(ListNode head) {
            if(head == null || head.next == null){
                return head;
            }
    
            ListNode slow = head;
            ListNode fast = head;
    
            //判断
            while(fast.next != null && fast.next.next != null){
                fast = fast.next.next;
                slow = slow.next;
            }
    
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • 3_🔥 永久会员 2023年7月9日 下午10:04

    ####删除靠左边的结点,关键点:while(fast.next.next != null)

    class Solution {
        //找出偏左的中间节点
        public static ListNode middleNode2(ListNode head){
            ListNode slow = head;
            ListNode fast = head;
            while(fast != null && fast.next.next != null){
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • Mr.肖 永久会员 2023年7月6日 下午8:35

    打卡:
    删除靠左边的结点,关键点:while(fast.next.next != null)

    class Solution {
        //找出偏左的中间节点
        public static ListNode middleNode2(ListNode head){
            ListNode slow = head;
            ListNode fast = head;
            while(fast != null && fast.next.next != null){
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • KEV 永久会员 2023年6月6日 下午4:13
    class Solution {
    public:
        ListNode* middleFirstNode(ListNode* head) {
            if(head==nullptr || head->next==nullptr){
                return head;
            }
            ListNode* fast=head,*slow=head;
    //此时快指针可以前进的条件是:当前快指针的下一个结点和当前快指针的下下一个结点都非空。
            while(fast->next && fast->next->next){
                fast=fast->next->next;
                slow=slow->next;
            }
            return slow;
        }
    };
    
    0 赞同 0 条回复
  • LicodeCO.LTDao 普通 2023年5月20日 下午4:43
    function middleFirstNode(head: ListNode | null): ListNode | null {
      // 边界判断: 空链表或只有一个节点时
      if (head === null || head.next === null) {
        return head;
      }
    
      // 定义快指针,初始指向头结点
      let fast = head;
      // 定义慢指针,初始指向头结点
      let slow = head;
    
      // 中间第一个节点
      while (fast.next !== null && fast.next.next !== null) {
        slow = slow.next;
        fast = fast.next.next;
      }
    
      return slow;
    }
    
    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年5月16日 下午8:01
    //相比另一种判断方式,结点为偶数个时,快指针少走一步,所以慢指针也会少走一步,停在中间偏左
    class Solution {
        public ListNode middleNode(ListNode head) {
            if(head == null || head.next == null){
                return head;
            }
            ListNode low = head;
            ListNode fast = head;
            while(fast.next != null && fast.next.next != null){
                low = low.next;
                fast = fast.next.next;
            }
            return low;
        }
    }
    
    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年4月25日 下午8:58
    //如下
    class Solution {
        public ListNode middleFirstNode(ListNode head) {
            ListNode slow = head;
            ListNode fast = head;
            while(fast.next != null && fast.next.next != null)
            {
                slow = slow.next;
                fast = fast.next;
                fast = fast.next;
            }
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • Only You!(^-^(^-^)) 永久会员 2023年4月20日 下午4:27
    /*给你单链表的头结点 head ,请你找出并返回链表的中间结点。
        * 如果有两个中间结点,则返回第一个中间结点。*/
        ListNode middleFirstNode(ListNode head){
            if (head == null) return null;
            ListNode slow = head, fast = head;
            //因为fast快,判断fast是否到达末尾,两种情况
            // 第一种,节点个数为奇数,例如:1,2,3,要判断fast.next != null
            //第二种,节点个数为偶数,因为返回中间节点第一个节点,要判断fast.next.next != null
            // 例如:1,2,3,4,返回2
            while (fast.next != null && fast.next.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            }
            return slow;
        }
    
    0 赞同 0 条回复
  • 庄重 普通 2023年4月18日 下午7:25
    class Solution {
    public:
        ListNode* middleNode(ListNode* head) {
            // 判断特殊情况
            if(head == nullptr || head->next == nullptr) return head;
    
            ListNode *fast = head;
            ListNode *slow = head;
            // 偶数时返回第二个节点
            while(fast != nullptr && fast->next != nullptr) {
            // 偶数时返回第一个节点
            while(fast->next != nullptr && fast->next->next != nullptr) {
                fast = fast->next->next;
                slow = slow->next;
            }
            return slow;
        }
    };
    // 时间复杂度O(n);
    // 空间复杂度O(1);
    
    0 赞同 0 条回复
  • 有去过的人 普通 2023年4月18日 上午11:07
    class Solution {
        public ListNode middleNode(ListNode head) {
            if(head==null||head.next==null) {
                return head;
            }
            ListNode slow=head;//一次一步
            ListNode fast=head;//一次两步
            while(fast.next!=null && fast.next.next!=null ) {//注意前后问题
                slow=slow.next;
                fast=fast.next.next;
            }
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • 小趴菜 永久会员 2023年4月17日 下午7:01

    //如果换成虚拟头结点写法,因为一开始的虚拟节点引入使得偶变奇,写法恢复
    class Solution {
    public ListNode middleNode(ListNode head) {
    if(head null || head.next null){
    return head;
    }
    ListNode slow = head;
    ListNode fast = head;
    while(fast.next!=null&&fast.next.next!=null){
    fast = fast.next.next;
    slow = slow.next;
    }
    return slow;
    }
    }

    0 赞同 0 条回复
  • 小肸 永久会员 2023年4月14日 下午7:55
    class Solution {
        public ListNode middleNode(ListNode head) {
            ListNode dummyHead = new ListNode(-1);
            dummyHead.next = head;
            ListNode slow = dummyHead, fast = dummyHead;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            //加上下面代码,就是返回链表两个中间节点的第二个节点
            // if (fast != null) {
            //     slow = slow.next;
            // }
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • Bryce 普通 2023年4月12日 下午11:37

    时间复杂度O(n),额外空间复杂度O(1)

    func middleFirstNode(head *ListNode) *ListNode {
        if head == nil {
            return head
        }
        s, f := head, head
        for f.Next != nil {
            if f.Next.Next == nil {
                return s
            }
            f = f.Next.Next
            s = s.Next
        }
        return s
    }
    
    0 赞同 0 条回复
  • Violet 永久会员 2023年4月12日 下午9:46

    一

    class Solution {
        public ListNode middleNode(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode fast = head, slow = head;
            while (fast.next != null && fast.next.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年4月12日 下午7:26
        public ListNode middleNode(ListNode head) {
              if(head==null||head.next==null){
                return head;
              }
              ListNode slow =head;
              ListNode fast =head;
              /*
               * 奇数时fast一定会超出一个,只需要提前判断超出的那个位置即可取得第一个
               */
            while(fast!=null&&fast.next!=null&&fast.next!=null){
                slow =slow.next;
                fast= fast.next.next;
            }
            return slow;
    
        }
    
    0 赞同 0 条回复
  • Everglow 永久会员 2023年4月12日 上午10:22

    时间O(n)

    public ListNode middleFirstNode(ListNode head) {
            ListNode slow = head;
            ListNode fast = head;
            while(fast != null && fast.next!= null && fast.next.next != null) {
                fast = fast.next.next;
                slow = slow.next; 
            }
            return slow;
        }
    
    0 赞同 1 条回复
    • shuaidi 永久会员 @ Everglow 2023年4月12日 下午1:14

      如果你在开头保证了head不为空的话,那么在while里面,其实可以不用判断fast!=null哦,因为 fast.next.next不为空,那么进入循环赋值之后,fast一定不为空。

  • 秊 永久会员 2023年4月12日 上午8:57

    时间:$O(n)$
    空间:$O(1)$

    class Solution {
        public ListNode middleNode(ListNode head) {
            ListNode fast = head, low = head;
            while (low.next != null && fast.next != null && fast.next.next != null) {
                low = low.next;
                fast = fast.next.next;
            }
    
            // if (fast.next == null) {
            //     return low;
            // } else {
            //     return low.next;
            // }
            return low;
        }
    }
    
    0 赞同 0 条回复
  • 月诉长安 永久会员 2023年4月11日 下午7:31

    解:

    ListNode* middleFirstNode(ListNode head){
     ListNode* headVir = new ListNode;
                headVir->next = head;
                ListNode* fast = headVir;
                ListNode* slow = headVir;
                while(fast != nullptr && fast->next != nullptr)
                {
                    fast = fast->next->next;
                    slow = slow->next;
                }
               delete headVir;
               return slow;
    
    
    }
    
    
    0 赞同 0 条回复
  • C++小将 永久会员 2023年4月11日 下午2:04
    打卡

    时间复杂度O(n),空间复杂度O(1)

    class Solution {
    public:
        ListNode* middleFirstNode(ListNode* head) {
            ListNode *slow = head;
            ListNode *fast = head;
            while(fast->next != nullptr && fast->next->next !=nullptr) {
                slow = slow->next;
                fast = fast->next->next;
            }
            return slow;
        }
    };
    
    0 赞同 0 条回复
  • 是梦终空 永久会员 2023年4月11日 上午11:49

    时间复杂度:O(n)

    class Solution {
        public ListNode middleNode(ListNode head) {
            ListNode fast = head;
            ListNode slow = head;
            while(fast != null &&fast.next != null){
                fast = fast.next.next;
                slow = slow.next;
                cur = cur.next;
            }
            return cur;
        }
    
        ListNode middleFirstNode(ListNode head){
            ListNode fast = head;
            ListNode slow = head;
            while(fast != null &&fast.next != null && fast.next.next != null){  //让快指针的下一个节点和下下一个节点都非空,在慢指针指到第一个节点时  循环就会结束
                fast = fast.next.next;
                slow = slow.next;
            }
            return slow;
        }
    }
    
    0 赞同 0 条回复

# 回答此问题

后才能回答