[LeetCode] 876. Middle of the Linked List_Easy tag: Linked List

Given a non-empty, singly linked list with head node head, return a middle node of linked list.

If there are two middle nodes, return the second middle node.

Example 1:

Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3.  (The judge‘s serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.

Example 2:

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.

Note:

  • The number of nodes in the given list will be between 1 and 100.

这个题目暴力做法就是将list走一遍,然后记录个数为n,接着再走n/2步即可。

但是这里给出用fast/slow pointers的做法,也就是用两个指针,fast一次走两步,slow一次走一步。如果fast.next is None, return slow; if fast.next.next is None, return slow.next(可以自己用一个小例子来判断就好)

Code

class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def middleList(self, head):
        # if not head: return head   #dont need because length belongs [1, 100]
        slow, fast = head, head
        while fast:
            if fast.next is None:
                return slow
            if fast.next.next is None:
                return slow.next
            slow = slow.next
            fast = fast.next.next
    

原文地址:https://www.cnblogs.com/Johnsonxiong/p/10801722.html

时间: 2024-08-01 07:20:16

[LeetCode] 876. Middle of the Linked List_Easy tag: Linked List的相关文章

[LeetCode] 876. Middle of the Linked List

Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. Example 1: Input: [1,2,3,4,5] Output: Node 3 from this list (Serialization: [3,4,5]) The ret

LeetCode 876 Middle of the Linked List 解题报告

题目要求 Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. 题目分析及思路 题目给出一个非空单链表,要求返回链表单的中间结点.可以将链表中的结点保存在list中,直接获取中间结点的索引即可. python代码 # Definition

[LeetCode] 138. Copy List with Random Pointer_Medium tag: Linked List

这个题目有两种做法, note: 注意head.random有可能是None的情况 1. S: O(n)    create一个dictionary,然后先建dictionary和copy list以及next指针.然后再一遍,去建random指针. 2: S: O(1)    利用如下图所示的结构,然后再将copy node和原来的node分开即可. Old List: A --> B --> C --> D InterWeaved List: A --> A' --> B

876. Middle of the Linked List - LeetCode

Question 876.?Middle of the Linked List Solution 题目大意:求链表的中间节点 思路:构造两个节点,遍历链接,一个每次走一步,另一个每次走两步,一个遍历完链表,另一个恰好在中间 Java实现: public ListNode middleNode(ListNode head) { ListNode slow = head; ListNode fast = head; while (fast != null) { fast = fast.next; i

LeetCode解题报告:Linked List Cycle && Linked List Cycle II

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

876. Middle of the Linked List

经典链表题找链表的中间节点 快慢指针 #include<iostream> #include<vector> #include<algorithm> using namespace std; //Definition for singly-linked list. //Given a non-empty, singly linked list with head node head, return a middle node of linked list. // //I

[LeetCode] 86. Partition List_Medium tag: Linked List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4->3-

[LeetCode] 21. Merge Two Sorted Lists_Easy tag: Linked List

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 这个题目思路就是用dummy node,然后依次判断是l

【LeetCode OJ 237】Delete Node in a Linked List

题目链接:https://leetcode.com/problems/delete-node-in-a-linked-list/ 题目:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the th