[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 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.

找单链表中点,一看到题就想到快慢指针

我的思路是开始fast, slow两个指针都在head处,fast每次走两步,slow每次走一步,再考虑到链表长度的奇偶可能导致fast走不了两步,加了个判断

ListNode* middleNode(ListNode* head) {
    if (head == NULL)
    {
        return head;
    }

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

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

    if (fast->next)
    {
        slow = slow->next;
    }

    return slow;
}

中间遇到下面这个问题,加上了几个NULL的判断就通过编译了

member access within null pointer of type ‘struct ListNode‘

再看看leetcode上其他人写的快慢指针,同样的快慢指针,这个明显要更好,不用考虑链表长度

ListNode* middleNode(ListNode* head) {
    if(head==NULL || head->next==NULL)
        return head;
    ListNode* slow=head;
    ListNode* fast=head;
    fast=fast->next->next;
    while(fast!=NULL && fast->next!=NULL){
        slow=slow->next;
        fast=fast->next->next;
    }
    return slow->next;
}

References

  1. LeetCodeBug-member access within null pointer of type ‘struct ListNode‘

原文地址:https://www.cnblogs.com/arcsinw/p/9397682.html

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

[LeetCode] 876. Middle of the 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. 题目分析及思路 题目给出一个非空单链表,要求返回链表单的中间结点.可以将链表中的结点保存在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 - 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

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】【Python】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? 思路:笨办法是每个节点再开辟一个属性存放是否访问过,这样遍历一遍即可知道是否有环.但为了不增加额外的空间,可以设置两个指针,一个一次走一步,另一个一次走两步,如果有环则两个指针一定会再次相遇,反之则不会. # Definition for singly-linked list.

[leetcode]Flatten Binary Tree to Linked List @ Python

原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: 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 6Hints: If you notice carefully

LeetCode: Flatten Binary Tree to Linked List [114]

[题目] 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 click to show hints. [题意] 将一颗二叉树转化为链表,right充当next指针,元素顺序为先序遍历的循序.不使用额外的空间 [思路] 先对左子树链表化,然后将右子树链表化.然后

【leetcode刷题笔记】Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 判断一个链表是否有环. 题解: 设置两个指针p1和p2: p1每次走一步,p2每次走两步,如果在这个过程中,p2为空,则没有环:否则两个指针必然相遇,则有环: 接下来找环的起点,将p1挪动到链表起始,

[LeetCode] 160. Intersection of Two Linked Lists 解题思路

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