`{"id":"bf3f63e0-df36-480c-9352-999d3238fbf8","name":"Middle Of A Linked List","description":"given a singly linked list with head node head, return a middle node of linked list.\r\nif there is 2 mid node then return first mid node.\r\n","inputFormat":"1->2->3->4->5->6->7->null\r\n1->2->3->4->5->6->null","outputFormat":"4->5->6->7->null\r\n3->4->5->6->null\r\n","constraints":"0 &lt;= N &lt;= 10^6","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *midNode(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n\r\n ListNode *head = midNode(dummy->next);\r\n while (head != nullptr)\r\n {\r\n cout << head->val << \" \";\r\n head = head->next;\r\n }\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode midNode(ListNode head) {\r\n \r\n return null;\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n ListNode head = midNode(dummy.next);\r\n while (head != null) {\r\n System.out.print(head.val + \" \");\r\n head = head.next;\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n1 2 3 4 5 6 7","sampleOutput":"4 5 6 7 ","questionVideo":"https://www.youtube.com/embed/aiFAbMOHqIc","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"de9f3566-d1ab-40e1-ae48-848f1823f196","name":"Middle Of A Linked List","slug":"middle-of-a-linked-list","type":1}],"next":{"id":"4f79ecf1-4601-42d6-b317-4ed431db37d8","name":"Middle Of A Linked List MCQ","type":0,"slug":"middle-of-a-linked-list-mcq"},"prev":{"id":"93133504-f1f9-4fd2-919d-b2da4cb9cb87","name":"Reverse a linked list","type":3,"slug":"reverse-a-linked-list"}}}`

# Middle Of A Linked List

given a singly linked list with head node head, return a middle node of linked list. if there is 2 mid node then return first mid node.

`{"id":"bf3f63e0-df36-480c-9352-999d3238fbf8","name":"Middle Of A Linked List","description":"given a singly linked list with head node head, return a middle node of linked list.\r\nif there is 2 mid node then return first mid node.\r\n","inputFormat":"1->2->3->4->5->6->7->null\r\n1->2->3->4->5->6->null","outputFormat":"4->5->6->7->null\r\n3->4->5->6->null\r\n","constraints":"0 &lt;= N &lt;= 10^6","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *midNode(ListNode *head)\r\n{\r\n return nullptr;\r\n}\r\n\r\nint main()\r\n{\r\n int n;\r\n cin >> n;\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n\r\n ListNode *head = midNode(dummy->next);\r\n while (head != nullptr)\r\n {\r\n cout << head->val << \" \";\r\n head = head->next;\r\n }\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode midNode(ListNode head) {\r\n \r\n return null;\r\n }\r\n\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n ListNode head = midNode(dummy.next);\r\n while (head != null) {\r\n System.out.print(head.val + \" \");\r\n head = head.next;\r\n }\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"7\r\n1 2 3 4 5 6 7","sampleOutput":"4 5 6 7 ","questionVideo":"https://www.youtube.com/embed/aiFAbMOHqIc","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"de9f3566-d1ab-40e1-ae48-848f1823f196","name":"Middle Of A Linked List","slug":"middle-of-a-linked-list","type":1}],"next":{"id":"4f79ecf1-4601-42d6-b317-4ed431db37d8","name":"Middle Of A Linked List MCQ","type":0,"slug":"middle-of-a-linked-list-mcq"},"prev":{"id":"93133504-f1f9-4fd2-919d-b2da4cb9cb87","name":"Reverse a linked list","type":3,"slug":"reverse-a-linked-list"}}}`

Editor

# Middle Of A Linked List

easy

given a singly linked list with head node head, return a middle node of linked list. if there is 2 mid node then return first mid node.

0 <= N <= 10^6

## Format

### Input

1->2->3->4->5->6->7->null 1->2->3->4->5->6->null

### Output

4->5->6->7->null 3->4->5->6->null

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}7 1 2 3 4 5 6 7```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}4 5 6 7 `

Question Video

Discussions

Show Discussion

Related Resources