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;
        if(fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
    }
    return slow;
}

原文地址:https://www.cnblogs.com/okokabcd/p/9406821.html

时间: 2024-07-29 09:42:44

876. Middle of the Linked List - LeetCode的相关文章

[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] 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 ret

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

Flatten Binary Tree to Linked List leetcode java

题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题解:如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空.注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子.整个程序需要维护一个全局变量,保

Palindrome Linked List Leetcode

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 这个follow up要求O(n)的时间和O(1)的空间,可以先reverse一半,然后再对比.只是reverse的时候要考虑奇数个还是偶数个.如果是奇数个的话,就跳过最中间的,从下一个开始reverse. /** * Definition for singly-li

Intersection of Two Linked Lists leetcode

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

Flatten Binary Tree to Linked List &lt;leetcode&gt;

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 算法:该题用的是递归,只要考虑当前节点,思路如下,设当前节点为root,把root的左节点的最右节点的右子树指向root的右子树,然后把root的右指针指向root的左子树,root的做指针置空,下一个节点处理r

Flatten Binary Tree to Linked List ——LeetCode

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 题目大意:给一个二叉树,将它转为一个list,转换后的序列应该是先序遍历,list由这棵树的右孩子表示. 解题思路:递归的处理左子树,然后处理右子树,将左孩子的右孩子置为当前节点的右孩子,然后将当前节点的右孩子置