Open azl397985856 opened 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)
思路 快慢指针
代码
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)
/**
* @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;
};
复杂度分析不是很会,不一定对,如果有错,请指正。
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
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
双指针 + 快慢指针,
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
};
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
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;
}
}
快慢指针,注意边界和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;
}
}
复杂度分析
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;
}
};
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];
}
};
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;
}
}
复杂度分析
快慢指针
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
复杂度分析
# 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
快慢指针移动,返回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
思路
代码
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
复杂度分析
https://leetcode.com/problems/middle-of-the-linked-list/
/**
* 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;
}
}
Two Pointers
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;
}
}
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
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;
}
};
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
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; } }
快慢指针,快指针每次走两步到尾部,慢指针每次走一步正好到中间
# 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
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)
快慢指针
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
# 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
快慢指针,因为是链表题,而且出现了链表中点,联想到快慢指针是很自然的。
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;
}
}
双指针
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 为数组长度。
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;
}
Two pointers. Fast pointer every time move two steps. Slow pointer just moves one step
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
Time: O(N) Space: O(1)
快慢指针
/**
* 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;
}
};
复杂度分析
链表,中间点,快慢指针
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)
//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)
将链表依次放入新数组,取数组的中间元素
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)]
};
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;
}
}
Time: O(N) Space: O(1)
快慢指针
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
快慢指针
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;
}
}
快慢指针
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)
思路:快慢指针。注意:判断条件要加上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
思路: 快慢指针法
复杂度分析:
代码(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;
}
};
快慢链表
/**
* 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;
}
}
快慢指针,快指针速度是慢指针的两倍
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 为数组长度。
快慢指针,快指针每次走两步,慢指针每次走一步,快指针走到末尾时,慢指针走到中间。
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
复杂度分析
思路:
慢指针步长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);
直接使用快慢指针就可以了,
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)
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
比较弄不清楚的是如果有两个中间节点,要怎么返回第二个节点:
首先是快慢指针,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;
}
}
var middleNode = function (head) {
let fast, slow;
fast = slow = head;
while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
};
快慢指针
var middleNode = function(head) {
let fast = slow = head;
while(fast && fast.next){
fast = fast.next.next;
slow = slow.next;
}
return slow;
};
快慢指针
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)
876. 链表的中间结点
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/middle-of-the-linked-list/
前置知识
暂无
题目描述