From bbe2b0bc0b2eece3acaa840ec34c624e93ccc798 Mon Sep 17 00:00:00 2001 From: Coding-Demon <93517332+Coding-Demon@users.noreply.github.com> Date: Sat, 29 Oct 2022 23:18:30 +0530 Subject: [PATCH] Create Find the Middle Element Of a Linked List.cpp Code for finding middle element of a linked list My code has passed all the testcases of LeetCode. --- ...ind the Middle Element Of a Linked List.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Linked List/Find the Middle Element Of a Linked List.cpp diff --git a/Linked List/Find the Middle Element Of a Linked List.cpp b/Linked List/Find the Middle Element Of a Linked List.cpp new file mode 100644 index 0000000..59cb81e --- /dev/null +++ b/Linked List/Find the Middle Element Of a Linked List.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + ListNode* middleNode(ListNode* head) { + ListNode * t = head; + ListNode * r= head->next; + while(r!=NULL && r->next!=NULL) + { + t=t->next; + r=r->next; + r=r->next; + } + + if(r==NULL) + return t; + return t->next; + + } +};