File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+
3+ Author : Saransh Bangar
4+ Date : 07/03/2024
5+ Problem : Middle of the Linked List
6+ Difficulty : Easy
7+ Problem Link : https://leetcode.com/problems/middle-of-the-linked-list/description/
8+ Video Solution : NA
9+
10+ */
11+
12+
13+ /* *
14+ * Definition for singly-linked list.
15+ * struct ListNode {
16+ * int val;
17+ * ListNode *next;
18+ * ListNode() : val(0), next(nullptr) {}
19+ * ListNode(int x) : val(x), next(nullptr) {}
20+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
21+ * };
22+ */
23+ class Solution {
24+ public:
25+ ListNode* middleNode (ListNode* head)
26+ {
27+ ListNode* temp=head;
28+ int count=0 ;
29+ while (temp!=nullptr )
30+ {
31+ count++;
32+ temp=temp->next ;
33+ }
34+ temp=head;
35+ int ans=count/2 ;
36+ while (ans)
37+ {
38+ temp=temp->next ;
39+ ans--;
40+ }
41+ return temp;
42+ }
43+ };
You can’t perform that action at this time.
0 commit comments