Skip to content

Commit aedcc52

Browse files
Merge pull request matthewsamuel95#537 from curlynoa/patch-2
Middle Node in Singly Linked List
2 parents 5d8a664 + ef2a1e9 commit aedcc52

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Data Structures/Linked List/SinglyLinkedList.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ void PrintLL(Node* head){
4848
}
4949
cout<<endl;
5050
}
51+
52+
Node* middleElement(Node* head) {
53+
Node* slowPtr = head;
54+
Node* fastPtr = head;
55+
while(fastPtr != nullptr && fastPtr->next != nullptr) {
56+
slowPtr = slowPtr->next;
57+
fastPtr = fastPtr->next->next;
58+
}
59+
return slowPtr;
60+
}
61+
5162
int main(){
5263
//create the linked list
5364
Node* head=takeInput();
@@ -62,4 +73,4 @@ int main(){
6273
10 20 30 40 50
6374
6475
Output:
65-
10 20 30 40 50*/
76+
10 20 30 40 50*/

0 commit comments

Comments
 (0)