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

 struct ListNode{
     int val;
     ListNode* next;
     ListNode(int x):val(x),next(NULL){} //初始化列表
 };
 class Solution{
 public:
     ListNode* middleNode(ListNode* head){
         if(head==NULL)
             return head;
         ListNode* slow=head;
         ListNode* fast=head;
         while(fast&&fast->next){
             fast=fast->next->next;
             slow=slow->next;
         }
         return slow;
     }
 };

int main(){
    vector<int> vals={1,2,3,4,5};
    ListNode* head=NULL;
    ListNode* cur=NULL;
    for(int i=0;i<vals.size();++i){
        if(cur==NULL){
            head=cur=new ListNode(vals[i]);
        }else{
            cur->next=new ListNode(vals[i]);
            cur=cur->next;
        }
    }

    Solution solution;
    ListNode* middle=solution.middleNode(head);
    std::cout<<middle->val<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/learning-c/p/9763871.html

时间: 2024-10-18 03:29:47

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

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

lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点

题目: 在O(1)时间复杂度删除链表节点 给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾).请在在O(1)时间复杂度删除该链表节点.并在删除该节点后,返回表头. 样例 给定 1->2->3->4,和节点 3,返回 1->2->4. 解题: 方法好贱,先把要删除节点后面一个节点的值赋给删除节点,这样需要删除节点就删除了,再把删除节点指向删除节点后面节点的节点 就像这样: node.val = node.next.val; node.next = node.next.

LeetCode contest-95[876,877,&#128065;878]

876. Middle of the Linked List first submission # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def middleNode(self, head): """ :type head: ListNode :rtype: L

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

234. Palindrome Linked List

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? public bool IsPalindrome(ListNode head) { if(head == null) return true; // Reverse the head; ListNode standForward = new ListNode(0);

九章算法系列(#5 Linked List)-课堂笔记

前言 又是很长时间才回来发一篇博客,前一个月确实因为杂七杂八的事情影响了很多,现在还是到了大火燃眉毛的时候了,也应该开始继续整理一下算法的思路了.Linked List大家应该是特别熟悉不过的了,因为这个算是数据结构了里面基本上最开始讲的结构吧.这块内容也没有太多需要琢磨的技巧,可以考量的东西也不多,所以考的就是一些小的trick来完成,面试中链表考得特别多,算是面试官对面试者的基础的考查,所以我建议大家在Linked List这一章,一定要实现Bug Free.这个也是我练的比较多的,有些想法