Advertisement
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:
- ListNode* deleteMiddle(ListNode* head) {
- if(head->next==NULL)
- {
- return NULL;
- }
- ListNode* temp = head;
- int l=0;
- while(temp->next!=NULL)
- {
- l++;
- temp=temp->next;
- }
- // if(l%2==1)
- // {
- // l=l/2;
- // }
- // else
- // {
- // l=(l/2)+1;
- // }
- int i=0;
- ListNode* prev=NULL;
- ListNode* curr=head;
- while(i<((l+1)/2))
- {
- prev=curr;
- curr=curr->next;
- i++;
- }
- // ListNode* curr=prev->next;
- prev->next=curr->next;
- curr->next=NULL;
- delete(curr);
- return head;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement