Modify Linked List
PROBLEM :
Given a singly linked list containing n nodes. Modify the value of first half nodes such that 1st node’s new value is equal to the last node’s value minus first node’s current value, 2nd node’s new value is equal to the second last node’s value minus 2nd node’s current value, likewise for first half nodes. If n is odd then the value of the middle node remains unchanged.
Note: Input in the linked list is like new node will be entered at the head position (1st position).
Input:
First line consists of T test cases. First line of every test case consists of N, denoting the number of nodes. Second line of every test case consists of Node of linked list.
Output:
Single line output, return the head of modified linked list.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
5
10 4 5 3 6
6
2 9 8 12 7 10
Output:
-4 -1 5 4 10
8 -2 4 8 9 2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Complete the function below
Node is as follows:
struct Node
{
int data;
struct Node* next;
};
*/
struct Node* reverse(struct Node* ) ;
struct Node* modifyTheList(struct Node *head)
{
if(!head||!head->next)
return head ;
struct Node *fast,*slow ;
fast=head->next ;
slow=head ;
while(fast&&fast->next)
{
fast=fast->next->next ;
slow=slow->next ;
}
struct Node* head2=slow->next ;
slow->next=NULL ;
head2=reverse(head2) ;
struct Node* temp ;
struct Node *ptr=head ;
temp=head2 ;
while(temp)
{
ptr->data=ptr->data-temp->data ;
temp=temp->next ;
ptr=ptr->next ;
}
head2=reverse(head2) ;
slow->next=head2 ;
return head ;
}
struct Node* reverse(struct Node* head)
{
if(!head||!head->next)
return head ;
struct Node* one,*two,*three ;
one=head ;
two=one->next ;
three=two->next ;
one->next=NULL ;
two->next=one ;
while(three)
{
one=two ;
two=three ;
three=three->next ;
two->next=one ;
}
return two ;
}
--------------------------------------------------------------------------------
Given a singly linked list containing n nodes. Modify the value of first half nodes such that 1st node’s new value is equal to the last node’s value minus first node’s current value, 2nd node’s new value is equal to the second last node’s value minus 2nd node’s current value, likewise for first half nodes. If n is odd then the value of the middle node remains unchanged.
Note: Input in the linked list is like new node will be entered at the head position (1st position).
Input:
First line consists of T test cases. First line of every test case consists of N, denoting the number of nodes. Second line of every test case consists of Node of linked list.
Output:
Single line output, return the head of modified linked list.
Constraints:
1<=T<=100
1<=N<=100
Example:
Input:
2
5
10 4 5 3 6
6
2 9 8 12 7 10
Output:
-4 -1 5 4 10
8 -2 4 8 9 2
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*Complete the function below
Node is as follows:
struct Node
{
int data;
struct Node* next;
};
*/
struct Node* reverse(struct Node* ) ;
struct Node* modifyTheList(struct Node *head)
{
if(!head||!head->next)
return head ;
struct Node *fast,*slow ;
fast=head->next ;
slow=head ;
while(fast&&fast->next)
{
fast=fast->next->next ;
slow=slow->next ;
}
struct Node* head2=slow->next ;
slow->next=NULL ;
head2=reverse(head2) ;
struct Node* temp ;
struct Node *ptr=head ;
temp=head2 ;
while(temp)
{
ptr->data=ptr->data-temp->data ;
temp=temp->next ;
ptr=ptr->next ;
}
head2=reverse(head2) ;
slow->next=head2 ;
return head ;
}
struct Node* reverse(struct Node* head)
{
if(!head||!head->next)
return head ;
struct Node* one,*two,*three ;
one=head ;
two=one->next ;
three=two->next ;
one->next=NULL ;
two->next=one ;
while(three)
{
one=two ;
two=three ;
three=three->next ;
two->next=one ;
}
return two ;
}
--------------------------------------------------------------------------------
Comments
Post a Comment