leetcode-pp / 91alg-6-daily-check

91 算法第六期打卡仓库
28 stars 0 forks source link

【Day 25 】2022-01-05 - 876. 链表的中间结点 #32

Open azl397985856 opened 2 years ago

azl397985856 commented 2 years ago

876. 链表的中间结点

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/middle-of-the-linked-list/

前置知识

暂无

题目描述

给定一个头结点为 head 的非空单链表,返回链表的中间结点。

如果有两个中间结点,则返回第二个中间结点。

 

示例 1:

输入:[1,2,3,4,5]
输出:此列表中的结点 3 (序列化形式:[3,4,5])
返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。
注意,我们返回了一个 ListNode 类型的对象 ans,这样:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.
示例 2:

输入:[1,2,3,4,5,6]
输出:此列表中的结点 4 (序列化形式:[4,5,6])
由于该列表有两个中间结点,值分别为 3 和 4,我们返回第二个结点。
 

提示:

给定链表的结点数介于 1 和 100 之间。
yan0327 commented 2 years ago

思路: 快慢指针 记住两个细节: 1.如果是 if fast != nil && fast.Next != nil 如果是偶数的链表个数指向靠后的中间链表 2.如果是 if fast.Next != nil && fast.Next.Next != nil 如果是偶数的链表个数指向靠前的中间链表

func middleNode(head *ListNode) *ListNode {
    if head == nil{
        return nil
    }
    slow,fast :=head,head
    for fast!=nil && fast.Next!=nil{
        slow = slow.Next
        fast = fast.Next.Next
    }
    return slow
}

时间复杂度O(N) 空间复杂度O(1)

yetfan commented 2 years ago

思路 快慢指针

代码

class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        slow = head
        fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

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

wxj783428795 commented 2 years ago

思路

  1. 快慢指针,快指针每次走两步,慢指针每次走一步
  2. 当快指针走到链表末尾,慢指针就在链表的中间节点

代码

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var middleNode = function (head) {
    let slow = head;
    let fast = head;
    while (fast !== null&& fast.next!==null){
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow;
};

复杂度分析

复杂度分析不是很会,不一定对,如果有错,请指正。

zwmanman commented 2 years ago
  1. fast and slow pointer, fast goes twice speed of slow
  2. when fast end, slow is the middle

class Solution(object): def middleNode(self, head): """ :type head: ListNode :rtype: ListNode """ if not head or not head.next: return head

    slow = head
    fast = head

    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    return slow
chakochako commented 2 years ago
class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        if not head:
            return None
        if not head.next:
            return head
        if not head.next.next:
            return head.next
        fast, slow = head, head
        while  fast and fast.next:
            fast = fast.next.next
            slow = slow.next

        return slow
codingPitaya commented 2 years ago

解题思路

双指针 + 快慢指针,slow 指针走一步,fast 指针走两步,当 fast 指针到链表终点时,slow 位于链表中间结点。

题解

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var middleNode = function(head) {
    let fast = head, slow = head;
    while(fast && fast.next){
        slow = slow.next;
        fast = fast.next.next;
    }
    return slow
};
zjsuper commented 2 years ago
class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = head
        fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow
wangzehan123 commented 2 years ago

代码

Java Code:


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        int length = 0;
        ListNode root = head;
        while(head != null){
            length++;
            head = head.next;
        }
        int n = 0;
        while(n<length/2){
            n++;
            root = root.next;
        }
        return root;
    }
}
hulichao commented 2 years ago

思路

快慢指针,注意边界和case 1.为null,为单head的情况 2.奇数情况 3,偶数情况

代码

class Solution {
    public ListNode middleNode(ListNode head) {
        if (head == null || head.next == null || head.next == null)
            return head;

        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }
}

复杂度分析

tian-pengfei commented 2 years ago
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        int n=1;
        ListNode *pos = head;
        ListNode *pos2 = head;
        while(pos->next){
            pos=pos->next;
            n++;
        }
        int mid = n/2;
        while(mid!=0){
            mid--;
            pos2=pos2->next;
        }
        return  pos2;
    }
};
bluetomlee commented 2 years ago
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        vector<ListNode*> A = {head};
        while (A.back()->next != NULL)
            A.push_back(A.back()->next);
        return A[A.size() / 2];
    }
};
LannyX commented 2 years ago

思路

2 pointers

代码

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null ){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}

复杂度分析

Bochengwan commented 2 years ago

思路

快慢指针

代码

class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        slow = fast = head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

复杂度分析

laofuWF commented 2 years ago
# slow, fast iterator
# time: O(N)
# space: O(1)
class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow = fast = head

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        return slow
CoreJa commented 2 years ago

思路

快慢指针移动,返回slow即可。

代码

class Solution:
    # 快慢指针移动,返回slow即可。
    def middleNode(self, head: ListNode) -> ListNode:
        slow, fast = head, head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow
feifan-bai commented 2 years ago

思路

  1. Two points, slow, fast with 1 step and 2 steps respectively.
  2. Slow reaches the midpoint when fast reaches the end.

代码

class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        slow, fast = head, head
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

复杂度分析

ginnydyy commented 2 years ago

Problem

https://leetcode.com/problems/middle-of-the-linked-list/

Note

Solution

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        if(head == null){
            return head;
        }

        ListNode slow = head;
        ListNode fast = head;

        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }
}

Complexity

XinnXuu commented 2 years ago

Notes

Two Pointers

Code

class Solution {
    public ListNode middleNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}

Complexity

rzhao010 commented 2 years ago

Thoughts Fast and slow pointers

Code

public ListNode middleNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

Complexity

zhangzz2015 commented 2 years ago

思路

关键点

代码

C++ Code:


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        if(head == NULL || head->next == NULL)
            return head; 

        ListNode* fast = head; 
        ListNode* slow = head; 

        while(fast && fast->next)
        {
            fast = fast->next->next; 
            slow = slow->next; 
        }

        return slow; 

    }
};
youzhaing commented 2 years ago

方法:快慢指针

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head: return None
        slow = fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next
        return slow

如果1234想得到2

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head: return None
        slow = fast = head
        while fast.next and fast.next.next:
            fast = fast.next.next
            slow = slow.next
        return slow
xinhaoyi commented 2 years ago

class Solution { public ListNode middleNode(ListNode head) { //快慢指针 ListNode slow = head; ListNode fast = head; while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; } return slow; } }

ZhangNN2018 commented 2 years ago

思路

快慢指针,快指针每次走两步到尾部,慢指针每次走一步正好到中间

复杂度

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow=fast=head
        while fast and fast.next:
            slow=slow.next
            fast=fast.next.next
        return slow
zhiyuanpeng commented 2 years ago
class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        f, s = head, head
        while f and f.next:
            f = f.next.next
            s = s.next
        return s

Time O(N) space O(1)

zhy3213 commented 2 years ago

思路

快慢指针

代码

    def middleNode(self, head: ListNode) -> ListNode:
        count=0
        fast,slow=head,head
        while fast.next:
            fast=fast.next
            count+=1
            if count & 1:
                slow=slow.next
        return slow
Laurence-try commented 2 years ago
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        p1, p2 = head, head
        while p2 and p2.next and p2.next.next:
            p1 = p1.next
            p2 = p2.next.next
        if p2.next:
            p1 = p1.next
        return p1
Yachen-Guo commented 2 years ago

思路

快慢指针,因为是链表题,而且出现了链表中点,联想到快慢指针是很自然的。

代码

class Solution {
    public ListNode middleNode(ListNode head) {
        if(head.next == null) return head;
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next!=null){
            if(fast.next.next == null) return slow.next;
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}
freesan44 commented 2 years ago

思路

双指针

关键点

代码

Python3 Code:


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        leftNode = head
        rightNode = head
        while rightNode.next:
            leftNode = leftNode.next
            if rightNode.next.next:
                rightNode = rightNode.next.next
            else:
                rightNode = rightNode.next
        return leftNode

复杂度分析

令 n 为数组长度。

moirobinzhang commented 2 years ago

Code:

public ListNode MiddleNode(ListNode head) {
    if (head == null)
        return head;

    ListNode slowNode = head;
    ListNode fastNode = head;

    while (fastNode != null && fastNode.next != null)
    {
        slowNode = slowNode.next;
        fastNode = fastNode.next.next;
    }        

    return slowNode;
}
ZacheryCao commented 2 years ago

Idea

Two pointers. Fast pointer every time move two steps. Slow pointer just moves one step

Code

class Solution:
    def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
        slow, fast = head, head
        while fast and fast.next:
            fast = fast.next.next
            slow = slow.next

        return slow

Complexity:

Time: O(N) Space: O(1)

Hacker90 commented 2 years ago

思路

快慢指针


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        if(head == nullptr) return nullptr;
        if(head->next == nullptr) return head;
        ListNode* slow = head;
        ListNode* fast = head;

        while(fast != nullptr && fast->next != nullptr) {
            slow = slow->next;
            fast =  fast->next->next;
        }
        cout<<"mid :"<<slow->val;
        return slow;
    }
};

复杂度分析

Yark-yao commented 2 years ago

思路

链表,中间点,快慢指针

代码

function middleNode(head: ListNode | null): ListNode | null {
    // 快慢指针
    if(head ===null || head.next === null){
        return head
    }
    let cur = head;
    let next = head.next
    while(next){
        cur= cur.next
        next = next.next
        next && (next = next.next)
    }
    return cur
};

复杂度

时间:O(N) 空间: O(1)

nonevsnull commented 2 years ago

思路

//s6

代码

//s6
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode fast = dummy, slow = dummy;

        while(fast != null){
            fast = fast.next;
            if(fast != null) {
                fast = fast.next;
            }
            slow = slow.next;
        }

        return slow;
    }
}

复杂度

time: O(N) space: O(1)

LinnSky commented 2 years ago

思路

将链表依次放入新数组,取数组的中间元素

代码

   var middleNode = function(head) {
    let newArr = [head]
    while(newArr[newArr.length - 1].next)  newArr.push(newArr[newArr.length - 1].next)
    return newArr[Math.floor(newArr.length/2)]
  };

复杂度分析

Husky-Gong commented 2 years ago

Code

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode slow = head, fast = head;

        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow;
    }
}

Complexity

Time: O(N) Space: O(1)

devosend commented 2 years ago

思路

快慢指针

代码

class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        slow, fast = head, head
        if not slow.next:
            return slow

        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        return slow

复杂度分析

CodingProgrammer commented 2 years ago

思路

快慢指针

代码

class Solution {
    public ListNode middleNode(ListNode head) {
        if (head == null) {
            return head;
        }

        ListNode slow = head, fast = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }
}

复杂度

biaohuazhou commented 2 years ago

思路

快慢指针

代码

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;
       }
       return slow;

    }
}

复杂度分析 时间复杂度: O(n) 空间复杂度:O(1)

hdyhdy commented 2 years ago

思路:快慢指针。注意:判断条件要加上fast == nil的情况,避免遍历到最后的节点判断fast.Next出现问题。

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func middleNode(head *ListNode) *ListNode {
    if head == nil {
        return nil
    }
    slow  := head 
    fast := head
    for fast != nil && fast.Next != nil  {
        slow = slow.Next
        fast = fast.Next.Next
    }
    return slow 
}

时间复杂度:n 空间复杂度:1

falconruo commented 2 years ago

思路: 快慢指针法

复杂度分析:

代码(C++):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        if (!head || !head->next) return head;

        ListNode* slow = head;
        ListNode* fast = head;

        while (fast && fast->next) {
            fast = fast->next->next;
            slow = slow->next;
        }

        return slow;
    }
};
BpointA commented 2 years ago

思路

快慢链表

代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode middleNode(ListNode head) {
        if(head==null || head.next==null)
        {
            return head;
        }
        ListNode quick=head;
        ListNode slow=head;
        ListNode pre=null;
        while (quick!=null && quick.next!=null)
        {
            quick=quick.next.next;
            pre=slow;
            slow=slow.next;
        }
        pre.next=null;
        return slow;
    }
}
iambigchen commented 2 years ago

思路

快慢指针,快指针速度是慢指针的两倍

关键点

代码

JavaScript Code:


/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var middleNode = function(head) {
    let fast = (slow = head)

    while(fast && fast.next) {
        fast = fast.next.next
        slow = slow.next
    }
    return slow
};

复杂度分析

令 n 为数组长度。

xuhzyy commented 2 years ago

思路

快慢指针,快指针每次走两步,慢指针每次走一步,快指针走到末尾时,慢指针走到中间。

代码

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head:
            return None

        fast = head
        slow = head
        while(fast and fast.next):
            slow = slow.next
            fast = fast.next.next
        return slow

复杂度分析

declan92 commented 2 years ago

思路:
慢指针步长1,快指针步长2
步骤:
while循环条件:快指针f不为null且f.next不为null,则慢指针后移1位,快指针后移2位;
返回慢指针索引指向结点为答案;
java

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode slow = head,fast = head;
        while(fast!=null&&fast.next!=null){
            slow = slow.next;
            fast = fast.next.next;            
        }
        return slow;
    }
}

时间:O(n),n为链表长度;
空间:O(1);

Zhang6260 commented 2 years ago

JAVA版本

直接使用快慢指针就可以了,

class Solution {
    public ListNode middleNode(ListNode head) {
        //快慢 指针呀
        ListNode slow=head,fast=head;
        while(fast!=null&&fast.next!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow;

    }
}

时间复杂度:O(n)

空间复杂度:O(1)

ZJP1483469269 commented 2 years ago
class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        f = head
        s = head
        while f and f.next :
            f = f.next.next
            s = s.next
        return s
qihang-dai commented 2 years ago

比较弄不清楚的是如果有两个中间节点,要怎么返回第二个节点:

首先是快慢指针,fast走两步,slow走一步。每轮slow比fast慢一步。如果fast是尾部节点(走完全部整个Length),slow走了半个length,比如 1 2 3 4 5 , fast走了 4步, slow 走了 2步, slow前后链表长度相等 离开循环的条件为 fast.next ! = null。 如果fast是尾部节点后一个空节点,脱离条件为fast != null, slow仍然走了fast的一半,而fast的位置比链表长了1格(相当于此时链表长度从偶数变回奇数),那么slow的位置自然还是这个原链表+1长度的中间节点,也就是原链表的第二个中间节点.


class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head, slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            //slow每次比fast慢一步
        }
        return slow;
    }
}
wenlong201807 commented 2 years ago

代码块

var middleNode = function (head) {
  let fast, slow;
  fast = slow = head;
  while (fast && fast.next) {
    fast = fast.next.next;
    slow = slow.next;
  }
  return slow;
};

时间复杂度和空间复杂度

shamworld commented 2 years ago

思路

快慢指针

代码

var middleNode = function(head) {
    let fast = slow = head;
    while(fast && fast.next){
       fast = fast.next.next;
        slow = slow.next;
    }
    return slow;
};
GaoMinghao commented 2 years ago

思路

快慢指针

代码

class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}

复杂度

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