# Linked List Weighted Sum In Reverse Order

{% embed url="<https://www.lintcode.com/problem/linked-list-weighted-sum-in-reverse-order/>" %}

## Solution 1 (Java)

```java
/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

public class Solution {

    int sum = 0;

    /**
     * @param head: the given linked list
     * @return: the array that store the values in reverse order 
     */
    public int weightedSumReverse(ListNode head) {
        reverseHelper(head);
        return sum;
    }

    private int reverseHelper(ListNode head) {
        if (head == null) return 0;
        int weight = reverseHelper(head.next);
        weight++;
        sum += weight * head.val;
        return weight;
    }
}
```

### Notes

* This is similar to [Reverse Order Storage](/algo/recursion-basics/reverse-order-storage.md). The difference is that we increment the weight and return it to its upper level each time.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.yushunchen.com/algo/recursion-basics/linked-list-weighted-sum-in-reverse-order.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
