Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode() : val(0), next(nullptr) {}
- * ListNode(int x) : val(x), next(nullptr) {}
- * ListNode(int x, ListNode *next) : val(x), next(next) {}
- * };
- */
- class Solution {
- public:
- int finleng(ListNode* head)
- {
- ListNode* tmp = head;
- int c=0;
- while(tmp!=nullptr)
- {
- c++;
- tmp=tmp->next;
- }
- return c;
- }
- ListNode* middleNode(ListNode* head) {
- int l=finleng(head);
- ListNode* temp = head;
- int nl = l/2;
- for(int i=0;i<nl;i++)
- {
- temp=temp->next;
- }
- return temp;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment