{"id":4642,"date":"2021-09-01T11:41:17","date_gmt":"2021-09-01T11:41:17","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4642"},"modified":"2023-07-31T04:49:42","modified_gmt":"2023-07-31T04:49:42","slug":"modify-contents-of-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/","title":{"rendered":"Modify contents of Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png\" alt=\"\" \/><\/p>\n<p>In this article, we will explore various approaches to update the value of a linked list in different programming languages, such as C, C++, Java, and Python. We will cover the step-by-step implementation of update algorithms, including iterative and recursive methods. Additionally, we will address common challenges, such as updating multiple occurrences of the same value and handling edge cases like updating the first or last node.<\/p>\n<h3>How to update value of linked list? <\/h3>\n<p>To understand this problem statement, let us take examples by referring the best online programming courses.<\/p>\n<p>If the given linked list is :<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/input.png\" alt=\"\" \/><\/p>\n<p>then according to the problem statement:<\/p>\n<ul>\n<li>At first, focus on the first and last node and change the value of the first node to <strong>(22 &#8211; 3) = 19<\/strong>.<\/li>\n<li>Then for the second node, its value will be updated to the value equal to the last-second node value minus the value of the second node, i.e., <strong>(18-12) = 6<\/strong>.<\/li>\n<li>Similarly, the value of the third node will be <strong>(5-1) = 4<\/strong>.<\/li>\n<li>So, the final linked list after modification will be: <\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/output.png\" alt=\"\" \/><\/p>\n<p>Let us take another example:<br \/>\nIf the linked list is 4\u21921\u219210\u219242\u21925\u2192NULL.<\/p>\n<ul>\n<li>After changing the values of the first half of <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/recursive-selection-sort-for-singly-linked-list-swapping-node-links\/\" title=\"nodes\">nodes<\/a> in the fashion mentioned above, our output linked list will be 1\u219241\u219210\u219242\u21925\u2192NULL<\/li>\n<li>Note that the number of nodes in the linked list is <strong>odd<\/strong>, so we will not change the <strong>middle<\/strong> node, i.e., node <strong>10<\/strong>.<\/li>\n<\/ul>\n<p>Now, I think the problem statement is clear, so let&#8217;s see how we can approach it. Any ideas? If not, it&#8217;s okay, and we will see how we can approach it in the next section.<\/p>\n<h3>Approach 1 to update value of linked list  <\/h3>\n<ul>\n<li>Find the first element of the second half of the linked list, i.e., find the middle node of the linked list.<\/li>\n<li>Push all elements starting from the node found above till the last of the linked list into a stack.<\/li>\n<li>Now using a temporary pointer, temp iterate the linked list starting from the head until the stack is not empty and modify <strong>temp\u2192data<\/strong> by doing <strong>temp\u2192data = (s.top() &#8211; temp\u2192data)<\/strong> and then pop the element from the stack.<\/li>\n<li>Finally, when the traversal is over, we will have our final modified list as per the problem statement.<\/li>\n<\/ul>\n<h3>Algorithm 1 to update value of linked list  <\/h3>\n<ul>\n<li>Initialize two pointers with the head of the list and name them <strong>slow<\/strong> and <strong>fast<\/strong>.<\/li>\n<li>Find the <strong>mid-node<\/strong> of the list by moving the <strong>slow<\/strong> pointer one node at a time and the <strong>fast<\/strong> pointer two nodes at a time, till <strong>fast<\/strong> or <strong>fast \u2192next<\/strong> are not NULL.<\/li>\n<li>Now once you reach the end, check whether the fast pointer is NULL or not\n<ul>\n<li>If it is NULL then the number of nodes in the list is <strong>even<\/strong>, then no need to update the <strong>slow<\/strong> pointer as it will point to the first element of the second half of the list.<\/li>\n<li>If it is not NULL that means the number of nodes in the list is <strong>odd<\/strong> so, to point to the first element of the second half of the list, we need to move the <strong>slow<\/strong> pointer by one node.<\/li>\n<\/ul>\n<\/li>\n<li>Now, we have the starting point of the second half of the list so, we will start iterating the list from here and push all data of nodes in the <strong>stack<\/strong> till the end of the list.<\/li>\n<li>Now, we will begin an iteration from the starting of the list and change the value of each node to the difference of the stack\u2019s top element and the current node at which we are in the iteration, and then we will pop the element from the stack.<\/li>\n<li>We will repeat the above step till the stack is not empty.<\/li>\n<\/ul>\n<p>We will get our desired list by following the above steps.<\/p>\n<p>**Time complexity:** O(n) is the time complexity for the Linked list value update, Where n is the number of nodes in the list.<br \/>\n**Space Complexity:** O(n) is the time complexity for the Linked list value update, Where n is the number of nodes in the list.<\/p>\n<p>As you can see that in the above approach, we used an extra space of O(n) because we were pushing the elements of the second half of the list in the stack.<\/p>\n<p>Can we improve this to constant extra space?<\/p>\n<ul>\n<li>The answer is yes, we can improve our algorithm, refer below to get an idea of the constant extra space approach.<\/li>\n<\/ul>\n<h4>Helpful Observations<\/h4>\n<ul>\n<li>Notice that we cannot move backward in a linked list, so we need to devise a way by which we can traverse the list in forward and backward directions.<\/li>\n<li>This can be only done if we first break the list from the middle into two parts and then reverse the second half.<\/li>\n<li>After doing this, we can treat each half as two different lists, and moving forward in the second half of the list would eventually mean to iterate in the list in the backward direction as it is reversed.<\/li>\n<li>Then, we can keep one pointer at the starting of each half, modify the nodes\u2019 values that belong to the first half, and advance each pointer to the next node.<\/li>\n<\/ul>\n<p>Let\u2019s see another approach on How to update value of linked list.<\/p>\n<h3>Approach 2 to update value of linked list  <\/h3>\n<p>Our approach will be simple:<\/p>\n<ul>\n<li>Find the middle node of the linked list and then <strong>split the linked list from the middle<\/strong> with the help of a <strong>fast<\/strong> and a <strong>slow<\/strong> pointer, where the <strong>slow<\/strong> will move one node at a time and the <strong>fast<\/strong> one will move 2 nodes at a time, so that when <strong>fast<\/strong> will be at the end of the list <strong>slow<\/strong> will be at the <strong>middle<\/strong>.<\/li>\n<li>Now, <strong>reverse the second half of the list<\/strong> obtained in the above step.<\/li>\n<li>Initialize two pointers named <strong>front<\/strong> and <strong>back<\/strong> with the starting of each list, respectively.<\/li>\n<li>Iterate over both the lists simultaneously and update the values of the first half nodes as directed in the problem statement.<\/li>\n<li>Then again, reverse the second half of the list.<\/li>\n<li>Then join the two parts (first half and second half) of the list as they were originally given to us.<\/li>\n<\/ul>\n<h3>Algorithm 2 to update value of linked list  <\/h3>\n<ul>\n<li>At first, we will find the middle node of our linked list and then break the list from there.<\/li>\n<li>Then, we will have two different linked lists.<\/li>\n<li>Then, we will reverse the second linked list.<\/li>\n<li>Initialize two pointers, <strong>front and back<\/strong>, where the <strong>front<\/strong> will point to the first node of the first list and the <strong>back<\/strong> will point to the first node of the second list.<\/li>\n<li>While the end of the second list is not reached i.e., <strong>back<\/strong> is not equal to NULL pointer, we need to iterate the list and do the following:<br \/>\na) Change front\u2019s data to the difference of back\u2019s and front\u2019s data <strong>(front-&gt;data = back-&gt;data &#8211; front-&gt;data)<\/strong>.<br \/>\nb) Advance front pointer by one node <strong>(front = front-&gt;next)<\/strong>.<br \/>\nc) Advance back pointer by one node <strong>(front = front-&gt;next)<\/strong>.<\/li>\n<li>After the while loop ends, reverse the second list again.<\/li>\n<li>Now, attach the second list to the end of the first list.<\/li>\n<li>Return the head of the newly created list.<\/li>\n<li>The first half nodes of the returned list will be modified according to the problem statement.<\/li>\n<\/ul>\n<h3>Dry Run of update value of linked list <\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Modify-contents-of-Linked-List-1.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Modify-contents-of-Linked-List-2.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Modify-contents-of-Linked-List-3.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4643 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_4643 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_4643 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4643 .wpsm_nav-tabs > li.active > a, #tab_container_4643 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4643 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_4643 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_4643 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4643 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4643 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4643 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4643 .wpsm_nav-tabs > li > a:hover , #tab_container_4643 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_4643 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_4643 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_4643 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_4643 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_4643 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_4643 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_4643 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4643 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_4643 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4643 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_4643 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_4643\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4643\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4643_1\" aria-controls=\"tabs_desc_4643_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4643_2\" aria-controls=\"tabs_desc_4643_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4643_3\" aria-controls=\"tabs_desc_4643_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4643_4\" aria-controls=\"tabs_desc_4643_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_4643\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_4643_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include<bits\/stdc++.h>\r\nusing namespace std;\r\nclass Node\r\n{\r\n    public:\r\n    int data;\r\n    Node* next;\r\n    Node(int x){\r\n        data = x;\r\n   next = NULL;\r\n    }\r\n};\r\n \r\n \r\nvoid midPartition(Node *head,\r\n               Node **front_ref, Node **back_ref)\r\n{\r\n    Node *slow, *fast;\r\n     \r\n    slow = head;\r\n    fast = head->next;\r\n     \r\n    \/* Advance 'fast' pointer two nodes at a time and\r\n       advance 'slow' pointer one node at a time *\/\r\n    while (fast != NULL)\r\n    {\r\n        fast = fast->next;\r\n        if (fast != NULL)\r\n        {\r\n            slow = slow->next;\r\n            fast = fast->next;\r\n        }\r\n    }\r\n     \r\n     \/* 'slow' is before the midpoint in the list,\r\n        so split it in two at that point. *\/\r\n    *front_ref = head;\r\n    *back_ref = slow->next;\r\n    slow->next = NULL;\r\n}\r\n\r\nvoid reverseList(Node **head_ref)\r\n{\r\n    Node *current, *prev, *next;\r\n    current = *head_ref;\r\n    prev = NULL;\r\n    while (current != NULL)\r\n    {\r\n        next = current->next;\r\n        current->next = prev;\r\n        prev = current;\r\n        current = next;\r\n    }   \r\n    *head_ref = prev;\r\n}\r\nvoid modifyContent(Node *head1,Node *head2)\r\n{\r\n    Node *front = head1, *back = head2;\r\n    \/\/ traverse both the lists simultaneously\r\n    while (back != NULL)\r\n    {\r\n        front->data = back->data - front->data;\r\n         \r\n        front = front->next;\r\n        back = back->next;\r\n    }\r\n}\r\n\r\nvoid printList(Node *head)\r\n{\r\n    if (!head)\r\n        return;\r\n     \r\n    while (head->next != NULL)\r\n    {\r\n        cout << head->data << \" \";\r\n        head = head->next;\r\n    }\r\n    cout << head->data << endl;\r\n}\r\n \r\nNode* concatFrontAndBackList(Node *front,\r\n                                    Node *back)\r\n{\r\n    Node *head = front;\r\n \r\n    while (front->next != NULL)\r\n        front = front->next;   \r\n         \r\n    front->next = back;\r\n     \r\n    return head;\r\n}\r\n\r\nNode* solve(Node *head)\r\n{\r\n \/\/ if list is empty or contains only single node then return it\r\n    if (!head || head->next == NULL)\r\n        return head;\r\n     \r\n    Node *front, *back;\r\n     \r\n    \/\/ split the list into two parts\r\n    midPartition(head, &front, &back);   \r\n         \r\n    \/\/ reverse the 2nd half of the list\r\n    reverseList(&back);\r\n     \r\n    \/\/ modify the contents of 1st half of the list  \r\n    modifyContent(front, back);\r\n         \r\n    \/\/ again reverse the 2nd half of list\r\n    reverseList(&back);\r\n     \r\n    \/\/ concatenate both the halves to get the original list\r\n    \/\/ that was provided initially\r\n    head = concatFrontAndBackList(front, back);\r\n     \r\n    \/\/ pointer to the modified list\r\n    return head;\r\n}\r\nint main(void)\r\n{\r\n    Node* head = NULL;\r\n    head = new Node(3);\r\n    head->next = new Node(12);\r\n    head->next->next = new Node(1);\r\n    head->next->next->next = new Node(5);\r\n    head->next->next->next->next = new Node(18);\r\n    head->next->next->next->next->next = new Node(22);\r\n    \r\n    Node *tmp = solve(head);\r\n    printList(tmp);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4643_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include <stdio.h>\r\n#include<stdbool.h>\r\n#include<stdlib.h>\r\n \r\nstruct Node {\r\n    int data;\r\n    struct Node* prev;\r\n    struct Node* next;\r\n};\r\n\r\nvoid push(struct Node** head_ref, int new_c)\r\n{\r\n    struct Node* new_node =\r\n            (struct Node*) malloc(sizeof(struct Node));\r\n    new_node->data = new_c;\r\n    new_node->prev = NULL;\r\n    new_node->next = (*head_ref);\r\n    if ((*head_ref) != NULL)\r\n        (*head_ref)->prev = new_node;\r\n     *head_ref = new_node;\r\n}\r\n\r\nbool isCircular(struct Node *head){\r\n struct Node *temp=head;\r\n while(temp!=NULL)\r\n { \/\/if temp points to head then it has completed a circle,thus a circular linked list.\r\n    if(temp->next==head)\r\n        return true;\r\n    temp=temp->next;\r\n}\r\n\r\nreturn false;\r\n\r\n}\r\nint main(void)\r\n{\r\n    struct Node* head = NULL;\r\n    push(&head, 5);\r\n    push(&head, 4);\r\n    push(&head, 3);\r\n    push(&head, 2);\r\n    push(&head, 1);\r\n    \r\n    if(isCircular(head))\r\n\t\tprintf(\"Yes&#92;n\");\r\n\telse\r\n\t\tprintf(\"No&#92;n\");\r\n \r\n}\r\n\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4643_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Modify\r\n{\r\n    static class Node\r\n    {\r\n\t    int data;\r\n\t    Node next;\r\n    };\r\n    static Node push(Node head_ref, int new_data)\r\n    {\r\n\t    Node new_node =new Node();\r\n        new_node.data = new_data;\r\n\t    new_node.next = head_ref;\r\n\t    head_ref = new_node;\r\n\t\r\n\t    return head_ref;\r\n    }\r\n    static Node front,back;\r\n    \/* Split the nodes of the given list into front and back halves,\r\n    and return the two lists using the reference parameters.\r\n    Uses the fast\/slow pointer strategy. *\/\r\n    static void frontAndBackSplit( Node head)\r\n    {\r\n\t    Node slow, fast;\r\n\t\r\n\t    slow = head;\r\n\t    fast = head.next;\r\n\t\r\n\t    \/* Advance 'fast' two nodes, and\r\n\t    advance 'slow' one node *\/\r\n\t    while (fast != null)\r\n\t    {\r\n\t\t    fast = fast.next;\r\n\t\t    if (fast != null)\r\n\t\t    {\r\n\t\t\t    slow = slow.next;\r\n\t\t\t    fast = fast.next;\r\n\t\t    }\r\n        }\r\n\t    \/* 'slow' is before the midpoint in the list,\r\n\t\tso split it in two at that point. *\/\r\n\t    front = head;\r\n\t    back = slow.next;\r\n\t    slow.next = null;\r\n    }\r\n    \/* Function to reverse the linked list *\/\r\n    static Node reverseList( Node head_ref)\r\n    {\r\n\t    Node current, prev, next;\r\n\t    current = head_ref;\r\n\t    prev = null;\r\n\t    while (current != null)\r\n\t    {\r\n\t\t    next = current.next;\r\n\t\t    current.next = prev;\r\n\t\t    prev = current;\r\n\t\t    current = next;\r\n\t    }\r\n\t    head_ref = prev;\r\n\t    return head_ref;\r\n    }   \r\n    \/\/ perform the required subtraction operation on the 1st half of the linked list\r\n    static void modifyTheContentsOf1stHalf()\r\n    {\r\n\t    Node front1 = front, back1 = back;\r\n\t    \/\/ traversing both the lists simultaneously\r\n\t    while (back1 != null)\r\n\t    {\r\n\t\t    \/\/ subtraction operation and node data modification\r\n\t\t    front1.data = front1.data - back1.data;\r\n\t\t    front1 = front1.next;\r\n\t\t    back1 = back1.next;\r\n\t    }\r\n    }\r\n    \/\/ function to concatenate the 2nd(back) list\r\n    \/\/ at the end of the 1st(front) list and\r\n    \/\/ returns the head of the new list\r\n    static Node concatFrontAndBackList(Node front,Node back)\r\n    {\r\n\t    Node head = front;\r\n\t\r\n\t    if(front == null)return back;\r\n\t\r\n\t    while (front.next != null)\r\n\t\t    front = front.next;\r\n\t\t\r\n\t    front.next = back;\r\n\t\r\n\t    return head;\r\n    }\r\n    \/\/ function to modify the contents of the linked list\r\n    static Node modifyTheList( Node head)\r\n    {\r\n\t    \/\/ if list is empty or contains only single node\r\n\t    if (head == null || head.next == null)\r\n\t\t    return head;\r\n\t    front = null; back = null;\r\n\t\r\n\t    \/\/ split the list into two halves front and back lists\r\n\t    frontAndBackSplit(head);\r\n\t\t\r\n\t    back = reverseList(back);\r\n\t    \/\/ modify the contents of 1st half\r\n\t    modifyTheContentsOf1stHalf();\r\n\t\r\n\t    \/\/ agains reverse the 2nd(back) list\r\n\t    back = reverseList(back);\r\n\r\n\t    head = concatFrontAndBackList(front, back);\r\n\r\n\t    return head;\r\n    }\r\n    static void printList( Node head)\r\n    {\r\n\t    if (head == null)\r\n\t        return;\r\n\t\r\n\t    while (head.next != null)\r\n\t    {\r\n\t\t    System.out.print(head.data + \" -> \");\r\n\t\t    head = head.next;\r\n\t    }\r\n\t    System.out.println(head.data );\r\n    }\r\n    \/\/ Driver Code\r\n    public static void main(String args[])\r\n    {\r\n\t    Node head = null;\r\n\t\r\n\t    \/\/ creating the linked list\r\n\t    head = push(head, 10);\r\n\t    head = push(head, 7);\r\n\t    head = push(head, 12);\r\n\t    head = push(head, 8);\r\n\t    head = push(head, 9);\r\n\t    head = push(head, 2);\r\n\t\r\n\t    \/\/ modify the linked list\r\n\t    head = modifyTheList(head);\r\n\t\r\n\t    \/\/ print the modified linked list\r\n\t    System.out.println( \"Modified List:\" );\r\n\t    printList(head);\r\n    }\r\n}\r\n\r\n\/\/ This code is contributed by Arnab Kundu\r\n\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4643_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\nclass Node:\r\n\t\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\ndef push(head_ref, new_data):\r\n\r\n\tnew_node =Node(0)\r\n\tnew_node.data = new_data\r\n\tnew_node.next = head_ref\r\n\thead_ref = new_node\r\n\treturn head_ref\r\n\r\nfront = None\r\nback = None\r\n\r\ndef frontAndBackSplit( head):\r\n\r\n\tglobal front\r\n\tglobal back\r\n\tslow = None\r\n\tfast = None\r\n\t\r\n\tslow = head\r\n\tfast = head.next\r\n\t\r\n\twhile (fast != None):\r\n\t\r\n\t\tfast = fast.next\r\n\t\tif (fast != None):\r\n\t\t\tslow = slow.next\r\n\t\t\tfast = fast.next\r\n\r\n\tfront = head\r\n\tback = slow.next\r\n\tslow.next = None\r\n\treturn head\r\n\r\ndef reverseList( head_ref):\r\n\r\n\tcurrent = None\r\n\tprev = None\r\n\tnext = None\r\n\tcurrent = head_ref\r\n\tprev = None\r\n\twhile (current != None):\r\n\t\r\n\t\tnext = current.next\r\n\t\tcurrent.next = prev\r\n\t\tprev = current\r\n\t\tcurrent = next\r\n\t\r\n\thead_ref = prev\r\n\treturn head_ref\r\n\r\ndef modifyTheContentsOf1stHalf():\r\n\r\n\tglobal front\r\n\tglobal back\r\n\tfront1 = front\r\n\tback1 = back\r\n\t\r\n\twhile (back1 != None):\r\n\t\r\n\t\tfront1.data = (back1.data - front1.data)\r\n\t\tfront1 = front1.next\r\n\t\tback1 = back1.next\r\n\t\r\ndef concatFrontAndBackList( front, back):\r\n\t\r\n\thead = front\r\n\t\r\n\tif(front == None):\r\n\t\treturn back\r\n\t\r\n\twhile (front.next != None):\r\n\t\tfront = front.next\r\n\t\t\r\n\tfront.next = back\r\n\treturn head\r\n\r\ndef modifyTheList( head):\r\n\r\n\tglobal front\r\n\tglobal back\r\n\r\n\tif (head == None or head.next == None):\r\n\t\treturn head\r\n\tfront = None\r\n\tback = None\r\n\t\r\n\tfrontAndBackSplit(head)\r\n\tback = reverseList(back)\r\n\tmodifyTheContentsOf1stHalf()\r\n\tback = reverseList(back)\r\n\thead = concatFrontAndBackList(front, back)\r\n\treturn head\r\n\r\ndef printList( head):\r\n\r\n\tif (head == None):\r\n\t\treturn\r\n\t\r\n\twhile (head.next != None):\r\n\t\r\n\t\tprint(head.data ,end=\" \")\r\n\t\thead = head.next\r\n\t\r\n\tprint(head.data )\r\n\r\nhead = None\r\n\t\r\nhead = push(head, 22)\r\nhead = push(head, 18)\r\nhead = push(head, 5)\r\nhead = push(head, 1)\r\nhead = push(head, 12)\r\nhead = push(head, 3)\r\n\t\r\nhead = modifyTheList(head)\r\n\t\r\nprintList(head)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_4643 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_4643 a\"),jQuery(\"#tab-content_4643\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h4>Output<\/h4>\n<p>19 6 4 5 18 22<\/p>\n<p><strong>Time complexity:<\/strong> O(n), Where n is the number of nodes in the list.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>So, in this blog, we have tried to write down the steps to modify a node in linked list in the most optimal way. We hope this article will enhance your knowledge.  If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n<p>## FAQ for update value of linked list<\/p>\n<p>**1. Can I update values in a linked list without traversing the entire list?**<br \/>\nIn most cases, updating a value in a linked list requires traversing the list until the target node is found. However, if you have additional data structures, such as hash tables or indexes, you may perform updates more efficiently by directly accessing the target node.<\/p>\n<p>**2. How do I handle updating values in a sorted linked list?**<br \/>\nUpdating values in a sorted linked list requires maintaining the sorted order after the update. You can either remove and re-insert the node with the updated value or directly modify the node&#8217;s value while keeping the list sorted.<\/p>\n<p>**3. What are the best practices for updating multiple occurrences of a value in a linked list?**<br \/>\nTo update multiple occurrences of a value, you need to traverse the entire list and update each matching node accordingly. Ensure you correctly update all instances of the value to avoid any inconsistencies in the linked list.<\/p>\n<p>**4. How do I handle updating the value of the first or last node in a linked list?**<br \/>\nUpdating the first node involves updating the head pointer to point to the new value. For updating the last node, you may need to traverse the list until the last node is reached and then update its value.<\/p>\n<p>**5. Can updating values in a linked list result in memory leaks or data corruption?**<br \/>\nUpdating values in a linked list should be done with caution to avoid memory leaks and data corruption. Always properly manage memory allocation and deallocation, and ensure that pointers are correctly updated to maintain the integrity of the linked list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will explore various approaches to update the value of a linked list in different programming languages, such as C, C++, Java, and Python. We will cover the step-by-step implementation of update algorithms, including iterative and recursive methods. Additionally, we will address common challenges, such as updating multiple occurrences of the same [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-4642","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Modify contents of Linked List | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn how to modify the elements of the linked list in the most optimal way. This blog explains how you can modify the contents of a linked list in the most optimal way.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Modify contents of Linked List | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn how to modify the elements of the linked list in the most optimal way. This blog explains how you can modify the contents of a linked list in the most optimal way.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-01T11:41:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-31T04:49:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Modify contents of Linked List\",\"datePublished\":\"2021-09-01T11:41:17+00:00\",\"dateModified\":\"2023-07-31T04:49:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/\"},\"wordCount\":1589,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/\",\"name\":\"Modify contents of Linked List | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png\",\"datePublished\":\"2021-09-01T11:41:17+00:00\",\"dateModified\":\"2023-07-31T04:49:42+00:00\",\"description\":\"Learn how to modify the elements of the linked list in the most optimal way. This blog explains how you can modify the contents of a linked list in the most optimal way.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Modify contents of Linked List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Modify contents of Linked List | Linked List | Prepbytes","description":"Learn how to modify the elements of the linked list in the most optimal way. This blog explains how you can modify the contents of a linked list in the most optimal way.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Modify contents of Linked List | Linked List | Prepbytes","og_description":"Learn how to modify the elements of the linked list in the most optimal way. This blog explains how you can modify the contents of a linked list in the most optimal way.","og_url":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-01T11:41:17+00:00","article_modified_time":"2023-07-31T04:49:42+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Modify contents of Linked List","datePublished":"2021-09-01T11:41:17+00:00","dateModified":"2023-07-31T04:49:42+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/"},"wordCount":1589,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/","name":"Modify contents of Linked List | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png","datePublished":"2021-09-01T11:41:17+00:00","dateModified":"2023-07-31T04:49:42+00:00","description":"Learn how to modify the elements of the linked list in the most optimal way. This blog explains how you can modify the contents of a linked list in the most optimal way.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644916445879-81modifycontent_Artboard%206.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/modify-contents-of-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Modify contents of Linked List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=4642"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4642\/revisions"}],"predecessor-version":[{"id":17412,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4642\/revisions\/17412"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}