Skip to content

Commit 34cb2e6

Browse files
committed
mid of LL
1 parent f4771b0 commit 34cb2e6

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

DSA Crack Sheet/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
- [Add two numbers represented by linked lists](https://practice.geeksforgeeks.org/problems/add-two-numbers-represented-by-linked-lists/1# "view question") - [Cpp Solution](./solutions/Add%20two%20numbers%20represented%20by%20linked%20lists.cpp)
136136
- [Intersection of two sorted Linked lists](https://practice.geeksforgeeks.org/problems/intersection-of-two-sorted-linked-lists/1# "view question") - [Cpp Solution](./solutions/Intersection%20of%20two%20sorted%20Linked%20lists.cpp)
137137
- [Intersection Point in Y Shapped Linked Lists](https://practice.geeksforgeeks.org/problems/intersection-point-in-y-shapped-linked-lists/1 "view question") - [Cpp Solution](./solutions/Intersection%20Point%20in%20Y%20Shapped%20Linked%20Lists.cpp)
138+
- [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/ "view question") - [Cpp Solution](./solutions/Middle%20of%20the%20Linked%20List.cpp)
139+
- []( "view question") - [Cpp Solution](./solutions/.cpp)
138140
- []( "view question") - [Cpp Solution](./solutions/.cpp)
139141

140142
### Binary Tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Middle of the Linked List
3+
=========================
4+
5+
Given a non-empty, singly linked list with head node head, return a middle node of linked list.
6+
7+
If there are two middle nodes, return the second middle node.
8+
9+
Example 1:
10+
Input: [1,2,3,4,5]
11+
Output: Node 3 from this list (Serialization: [3,4,5])
12+
The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
13+
Note that we returned a ListNode object ans, such that:
14+
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
15+
16+
Example 2:
17+
Input: [1,2,3,4,5,6]
18+
Output: Node 4 from this list (Serialization: [4,5,6])
19+
Since the list has two middle nodes with values 3 and 4, we return the second one.
20+
21+
Note:
22+
The number of nodes in the given list will be between 1 and 100.
23+
*/
24+
25+
/**
26+
* Definition for singly-linked list.
27+
* struct ListNode {
28+
* int val;
29+
* ListNode *next;
30+
* ListNode() : val(0), next(nullptr) {}
31+
* ListNode(int x) : val(x), next(nullptr) {}
32+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
33+
* };
34+
*/
35+
36+
class Solution
37+
{
38+
public:
39+
ListNode *middleNode(ListNode *head)
40+
{
41+
auto fast = head, slow = head;
42+
while (fast && fast->next)
43+
{
44+
fast = fast->next->next;
45+
slow = slow->next;
46+
}
47+
return slow;
48+
}
49+
};

0 commit comments

Comments
 (0)