Given a linked list. The task is to print the difference between the first odd-positioned node with the sum of all other odd-positioned nodes.
Examples:
Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42
Output : -48
Alternate nodes : 1 -> 3 -> 17 -> 29
1 – (3 + 17 + 29) = -48
Input : 10 -> 17 -> 33 -> 38 -> 73
Output : -96
Alternate nodes : 10 -> 33 -> 73
10 – (33 + 73) = -96
We have already discussed the sum of the alternative nodes of a linked list
Iterative Approach :
- Traverse the whole linked list.
- Set difference = 0 and count=0.
- Subtract the data of node from difference when count is even.
- Visit the next node.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
int subtractAlternateNode( struct Node* head)
{
int count = 0;
int difference = 0;
while (head != NULL) {
if (count % 2 == 0)
if (difference == 0){
difference = head->data;
}
else {
difference -= head->data;
}
count++;
head = head->next;
}
return difference;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
cout << subtractAlternateNode(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int subtractAlternateNode( struct Node* head)
{
int count = 0;
int difference = 0;
while (head != NULL) {
if (count % 2 == 0)
if (difference == 0){
difference = head->data;
}
else {
difference -= head->data;
}
count++;
head = head->next;
}
return difference;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*
) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
printf ( " %d " , subtractAlternateNode(head));
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Node head;
static class Node
{
int data;
Node next;
};
static int subtractAlternateNode(Node head)
{
int count = 0 ;
int difference = 0 ;
while (head != null )
{
if (count % 2 == 0 )
if (difference == 0 )
{
difference = head.data;
}
else
{
difference -= head.data;
}
count++;
head = head.next;
}
return difference;
}
static void push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
public static void main(String[] args)
{
head = null ;
push(head, 12 );
push(head, 29 );
push(head, 11 );
push(head, 23 );
push(head, 8 );
System.out.printf( " %d " , subtractAlternateNode(head));
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def subtractAlternateNode(head):
count = 0
difference = 0
while (head ! = None ) :
if (count % 2 = = 0 ):
if (difference = = 0 ):
difference = head.data
else :
difference = difference - head.data
count = count + 1
head = head. next
return difference
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
if __name__ = = '__main__' :
head = None
head = push(head, 12 )
head = push(head, 29 )
head = push(head, 11 )
head = push(head, 23 )
head = push(head, 8 )
print (subtractAlternateNode(head))
|
C#
using System;
class GFG
{
static Node head;
public class Node
{
public int data;
public Node next;
};
static int subtractAlternateNode(Node head)
{
int count = 0;
int difference = 0;
while (head != null )
{
if (count % 2 == 0)
if (difference == 0)
{
difference = head.data;
}
else
{
difference -= head.data;
}
count++;
head = head.next;
}
return difference;
}
static void push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
public static void Main(String[] args)
{
head = null ;
push(head, 12);
push(head, 29);
push(head, 11);
push(head, 23);
push(head, 8);
Console.WriteLine( " {0} " ,
subtractAlternateNode(head));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function subtractAlternateNode( head)
{
let count = 0;
let difference = 0;
while (head != null )
{
if (count % 2 == 0)
if (difference == 0)
{
difference = head.data;
}
else
{
difference -= head.data;
}
count++;
head = head.next;
}
return difference;
}
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
}
head = null ;
push(head, 12);
push(head, 29);
push(head, 11);
push(head, 23);
push(head, 8);
document.write(subtractAlternateNode(head));
</script>
|
Time Complexity: O(N), where N is the number of nodes in the linked list
Auxiliary Space: O(1)
Recursive Approach :
- Initialize a static variable(say flag).
- If flag is odd, subtract the node from difference.
- increase head and flag by 1, and recurse for next nodes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*)
malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void subtractAlternateNodes( struct Node* node,
int & difference, bool isOdd = true )
{
if (node == NULL)
return ;
if (isOdd == true ) {
if (difference == 0) {
difference = node->data;
}
else {
difference = difference - (node->data);
}
}
subtractAlternateNodes(node->next, difference,
!isOdd);
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
int difference = 0;
subtractAlternateNodes(head, difference);
cout << difference;
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node head;
static int difference;
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
return head;
}
static void subtractAlternateNodes(Node node,
boolean isOdd)
{
if (node == null )
return ;
if (isOdd == true )
{
if (difference == 0 )
{
difference = node.data;
}
else
{
difference = difference - (node.data);
}
}
subtractAlternateNodes(node.next, !isOdd);
}
public static void main(String[] args)
{
push(head, 12 );
push(head, 29 );
push(head, 11 );
push(head, 23 );
push(head, 8 );
difference = 0 ;
subtractAlternateNodes(head, true );
System.out.println(difference);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = next
def push(head_ref, new_data):
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
difference = 0 ;
def subtractAlternateNodes(node, isOdd ):
global difference
if (node = = None ):
return ;
if (isOdd = = True ):
if (difference = = 0 ) :
difference = node.data;
else :
difference = difference - (node.data);
subtractAlternateNodes(node. next , not isOdd);
head = None ;
head = push(head, 12 );
head = push(head, 29 );
head = push(head, 11 );
head = push(head, 23 );
head = push(head, 8 );
difference = 0 ;
subtractAlternateNodes(head, True );
print ( difference);
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node head;
static int difference;
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
return head;
}
static void subtractAlternateNodes(Node node,
Boolean isOdd)
{
if (node == null )
return ;
if (isOdd == true )
{
if (difference == 0)
{
difference = node.data;
}
else
{
difference = difference - (node.data);
}
}
subtractAlternateNodes(node.next, !isOdd);
}
public static void Main(String[] args)
{
push(head, 12);
push(head, 29);
push(head, 11);
push(head, 23);
push(head, 8);
difference = 0;
subtractAlternateNodes(head, true );
Console.WriteLine(difference);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
var head= null ;;
var difference = 0;
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
head = head_ref;
return head;
}
function subtractAlternateNodes(node, isOdd)
{
if (node == null )
return ;
if (isOdd == true )
{
if (difference == 0)
{
difference = node.data;
}
else
{
difference = difference - (node.data);
}
}
subtractAlternateNodes(node.next, !isOdd);
}
push(head, 12);
push(head, 29);
push(head, 11);
push(head, 23);
push(head, 8);
difference = 0;
subtractAlternateNodes(head, true );
document.write(difference);
</script>
|
Time Complexity: O(N) where N is number of nodes in the linked list
Auxiliary Space: O(N), for recursion call stack