Source : https://www.hackerrank.com/challenges/delete-a-node-from-a-linked-list
This challenge is part of a tutorial track by MyCodeSchool and is accompanied by a video lesson.
You’re given the pointer to the head node of a linked list and the position of a node to delete. Delete the node at the given position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The list may become empty after you delete the node.
Input Format
You have to complete the deleteNode(SinglyLinkedListNode* llist, int position)
method which takes two arguments - the head of the linked list and the position of the node to delete. You should NOT read any input from stdin/console. position
will always be at least 0 and less than the number of the elements in the list.
The first line of input contains an integer , denoting the number of elements in the linked list.
The next lines contain an integer each in a new line, denoting the elements of the linked list in the order.
The last line contains an integer denoting the position of the node that has to be deleted form the linked list.
Constraints
- , where is the element of the linked list.
Output Format
Delete the node at the given position and return
the head of the updated linked list. Do NOT print anything to stdout/console.
The code in the editor will print the updated linked list in a single line separated by spaces.
Sample Input
8206219741593
Sample Output
20 6 2 7 4 15 9
Explanation
The given linked list is 20->6->2->19->7->4->15->9
. We have to delete the node at position 3, which is 19. After deleting that node, the updated linked list is: 20->6->2->7->4->15->9
Source : https://www.hackerrank.com/challenges/delete-a-node-from-a-linked-list
Solution
// Karthikalapati.blogspot.com | |
/* | |
Delete Node at a given position in a linked list | |
Node is defined as | |
class Node { | |
int data; | |
Node next; | |
} | |
*/ | |
Node Delete(Node head, int position) { | |
if (head == null) { | |
return null; | |
} else if (position == 0) { | |
return head.next; | |
} else { | |
/* Get Node one element before desired position */ | |
Node n = head; | |
for (int i = 0; i < position - 1; i++) { | |
n = n.next; | |
} | |
/* Delete Node */ | |
n.next = n.next.next; | |
return head; | |
} | |
} |
2g2hgrwhtrth
ReplyDeleteYou’ve got some interesting points in this article. I would have never considered any of these if I didn’t come across this. Thanks!. packers and movers salem
ReplyDeleteAw, this became a very good post. In concept I must set up writing like this moreover – taking time and actual effort to produce a really good article… but exactly what can I say… I procrastinate alot and by no indicates apparently go completed. electronic accessories shop near me
ReplyDeleteI don’t know if it’s just me or if perhaps everyone else encountering problems with your blog. It seems like some of the written text within your content are running off the screen. Can someone else please provide feedback and let me know if this is happening to them as well? This could be a problem with my internet browser because I’ve had this happen previously. Many thanks 하이브슬롯
ReplyDeleteListcry is leading Car Transportation for House Shifting, Goods Transport, Cargo & Commercial Shifting Provider at best Charges . Packers and Movers Noida to Bangalore
ReplyDeleteThanks for this post, I am a big fan of this web site would like to proceed updated. 꽁머니커뮤니티
ReplyDelete“Good friends, good books, and a sleepy conscience: this is the ideal life. 웹툰사이트 추천
ReplyDeletewhoah this weblog is excellent i love reading your articles. Stay up the good paintings! You already know, lots of persons are looking round for this info, you can aid them greatly. επιλεξιμότητα για ινδική ηλεκτρονική βίζα πολίτες του Σαλβαδόρ
ReplyDelete