Home » C++ programs » D.S. programs

# Modify contents of Linked List using C++ program

**Modifying a Linked List**: In this article, we are going to learn **how to modify the content of a linked list using C++ program?**

Submitted by Souvik Saha, on May 06, 2019

**Problem statement:**

**Given a linked list, you modified that linked list** in such a way that the elements of the first half of that linked list are the difference of the first node to the last node and next node is the difference of the second node to the second last node and goes on.

**Example:**

Input: 4 → 5 → 9 → 3 → 8 → 1 → 2 → 5 Output: -1 → 3 → 8 → -5 → 8 → 1 → 2 → 5 Input: 6 → 5 → 4 → 9 → 1 → 2 → 7 Output: -1 → 3 → 3 → 9 → 1 → 2 → 7

**Algorithm:**

To solve the problem we can follow this algorithm:

- First, we find the midpoint of that linked list and take a copy of that.
- Then we divide that linked list from the middle point.
- Then reverse the second part of that linked list.
- Then calculate the difference of the first node of the two linked list and put the value to the original linked list.
- Continue to find the difference until we go to the last node of the second element.

**C++ implementation:**

#include <bits/stdc++.h> using namespace std; struct node { int data; node* next; }; void print(node*); //Create a new node struct node* create_node(int x) { struct node* temp = new node; temp -> data = x; temp -> next = NULL; return temp; } //Enter the node into the linked list void push(node** head, int x) { struct node* store = create_node(x); if (*head == NULL) { *head = store; return; } struct node* temp = *head; while (temp -> next) { temp = temp -> next; } temp -> next = store; } void split_list(node* head, node** a, node** b) { struct node* fast = head -> next; struct node* slow = head; while (fast != NULL && fast -> next != NULL) { slow = slow -> next; fast = fast -> next -> next; } struct node* temp = slow -> next; slow -> next = NULL; *a = head; *b = temp; } struct node* reverse(node* b) { struct node* curr = b; struct node* next = NULL; struct node* prev = NULL; while (curr != NULL) { next = curr -> next; curr -> next = prev; prev = curr; curr = next; } return prev; } void merge(node* a, node* b, node** c) { struct node* temp = a; *c = create_node(0); struct node* curr = *c; while (temp && b) { curr -> next = create_node(temp -> data - b -> data); b = b -> next; temp = temp -> next; curr = curr -> next; //cout<<curr->data<<" "; } if (b != NULL) { curr -> next = b; curr = curr -> next; } curr -> next = reverse(a); *c = (*c) -> next; } struct node* modifyTheList(struct node* head) { //add code here. struct node* a; struct node* b; struct node* c; split_list(head, &a, &b); struct node* temp = reverse(b); merge(temp, a, &c); return c; } //Print the list void print(node* head) { struct node* temp = head; while (temp) { cout << temp -> data << " "; temp = temp -> next; } } int main() { struct node* l = NULL; push(&l, 1); push(&l, 2); push(&l, 3); push(&l, 4); push(&l, 5); push(&l, 6); cout << "Before the modify operation" << endl; print(l); l = modifyTheList(l); cout << "\nAfter the modify operation" << endl; print(l); return 0; }

**Output**

Before the modify operation 1 2 3 4 5 6 After the modify operation 5 3 1 4 5 6

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.

Learn PCB Designing: PCB DESIGNING TUTORIAL