Open azl397985856 opened 1 year ago
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def middleNode(head):
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
# 测试样例
head = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node4 = ListNode(4)
node5 = ListNode(5)
head.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
result = middleNode(head)
while result:
print(result.val)
result = result.next
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func middleNode(_ head: ListNode?) -> ListNode? {
var slow = head
var fast = head
while fast != nil && fast?.next != nil {
fast = fast?.next?.next
slow = slow?.next
}
return slow
}
}
快慢指针: 快指针一次走两步 慢指针一次走一步
/**
* 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) {
if(!head) return head;
let slow = fast = head;
while(fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
};
复杂度分析
定义快慢指针,同时从头结点开始向后遍历,快指针每次走两步,慢指针每次走一步,当快指针走到末尾结点时,慢指针恰好走到中间结点。
class Solution:
def middleNode(self, head):
"""
:param head: ListNode
:return: ListNode
"""
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
return slow
复杂度分析
easy!
/**
* 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) {
if (!head) return head
let slow = fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
};
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func middleNode(head *ListNode) *ListNode {
slow, fast := head, head
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
}
return slow
}
需要输出中间节点后面所有的内容,所以用快慢指针,一个走一步,一个走两步,如果要输出中间节点就可以用左右端点指针,一个指向头,一个指向尾
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
#n=len(head)
left=right=head
#right=tail
while right and right.next:
left=left.next
right=right.next.next
return left
复杂度分析
快慢指针
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode* low = head;
ListNode* fast = head;
while (fast != nullptr && fast->next != nullptr)
{
low = low->next;
fast = fast->next->next;
}
return low;
}
};
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode* cur=head;
int cnt=0;
while(cur!=NULL){
cnt++;
cur=cur->next;
}
cur=head;
cnt=ceil(cnt/2);
while(cnt!=0){
cnt--;
cur=cur->next;
}
return cur;
}
};
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
slow = fast = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
return slow
/*
思路:
双指针
slow走一步,fast走两步
复杂度: 时间复杂度:O(n) 空间复杂度:O(1) */
// Definition for singly-linked list.
type ListNode struct {
Val int
Next *ListNode
}
func middleNode(head *ListNode) *ListNode {
slow, fast := head, head
for fast != nil && fast.Next != nil {
slow = slow.Next
fast = fast.Next.Next
}
return slow
}
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast ,slow = head , 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]:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
public ListNode middleNode(ListNode head) {
//快慢指针
ListNode fast = head;
ListNode slow = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
function middleNode(head: ListNode | null): ListNode | null {
if (head == null) {
return null;
}
let slow = head;
let fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
};
/**
* 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) {
auto p = head, q = head;
while (q && q->next) {
q = q->next;
p = p->next;
if (q) q = q->next;
}
//cout << p->val << endl;
return p;
}
};
双指针
/**
* @param {ListNode} head
* @return {ListNode}
*/
var middleNode = function (head) {
let slow = (fast = head);
while (slow && fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
};
时间复杂度: $O(N)$
空间复杂度: $O(1)$
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;
}
}
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; } }
class Solution {
public ListNode middleNode(ListNode head) {
if(head == null){
return head;
}
ListNode s = head, f = head;
while(f!=null && f.next !=null){
s = s.next;
f = f.next.next;
}
return s;
}
}
代码: /**
@return {ListNode} */ var middleNode = function(head) { let fast = head; let slow = head;
while (fast && fast.next) { 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) {
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
876. 链表的中间结点
入选理由
暂无
题目地址
https://leetcode-cn.com/problems/middle-of-the-linked-list/
前置知识
暂无
题目描述