mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
leetcode new problems
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
ListNode* removeNthFromEnd(ListNode* head, int n){
|
||||
if(head == nullptr) return head;
|
||||
ListNode* temp = head;
|
||||
int count = 0;
|
||||
while(temp!=nullptr) {
|
||||
count++;
|
||||
temp = temp->next;
|
||||
}
|
||||
if(n==count) {
|
||||
ListNode* newHead = head->next;
|
||||
delete head;
|
||||
return newHead;
|
||||
}
|
||||
temp = head;
|
||||
for(int i = 1; i<(count-n);i++){
|
||||
temp = temp->next;
|
||||
}
|
||||
ListNode* nodeToDelete = temp->next;
|
||||
temp->next = nodeToDelete->next;
|
||||
delete nodeToDelete;
|
||||
|
||||
return head;
|
||||
|
||||
}
|
||||
//n == count is when removal node is head
|
||||
//count-n just goes to one node before the removal node
|
||||
Reference in New Issue
Block a user