# Javascript Program To Find The Sum Of Last N Nodes Of The Given Linked List

• Last Updated : 13 Jan, 2022

Given a linked list and a number n. Find the sum of the last n nodes of the linked list.
Constraints: 0 <= n <= number of nodes in the linked list.

Examples:

```Input : 10->6->8->4->12, n = 2
Output : 16
Sum of last two nodes:
12 + 4 = 16

Input : 15->7->9->5->16->14, n = 4
Output : 44```

Method 1: (Recursive approach using system call stack)
Recursively traverse the linked list up to the end. Now during the return from the function calls, add up the last n nodes. The sum can be accumulated in some variable passed by reference to the function or to some global variable.

## Javascript

 ``

Output:

`Sum of last 2 nodes = 16`

Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(n), if system call stack is being considered.

Method 2 (Iterative approach using user-defined stack)
It is an iterative procedure to the recursive approach explained in Method 1 of this post. Traverses the nodes from left to right. While traversing pushes the nodes to a user-defined stack. Then pops the top n values from the stack and adds them.

## Javascript

 ``

Output:

`Sum of last 2 nodes = 16`

Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(n), stack size

Method 3 (Reversing the linked list)
Following are the steps:

1. Reverse the given linked list.
2. Traverse the first n nodes of the reversed linked list.
4. Reverse the linked list back to its original order.

## Javascript

 ``

Output:

`Sum of last 2 nodes = 16`

Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(1)

Method 4 (Using the length of linked list)
Following are the steps:

1. Calculate the length of the given Linked List. Let it be len.
2. First, traverse the (len – n) nodes from the beginning.
3. Then traverse the remaining n nodes and while traversing add them.

## Javascript

 ``

Output:

`Sum of last 2 nodes = 16`

Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(1)

Method 5 (Use of two pointers requires single traversal)
Maintain two pointers – reference pointer and main pointer. Initialize both reference and main pointers to head. First, move reference pointer to n nodes from head and while traversing accumulate node’s data to some variable, say sum. Now move both pointers simultaneously until the reference pointer reaches the end of the list and while traversing accumulate all node’s data to sum pointed by the reference pointer and accumulate all node’s data to some variable, say, temp, pointed by the main pointer. Now, (sum – temp) is the required sum of the last n nodes.

## Javascript

 ``

Output:

`Sum of last 2 nodes = 16`

Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(1)

Please refer complete article on Find the sum of last n nodes of the given Linked List for more details!

My Personal Notes arrow_drop_up