Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SumList
- {
- public static void transformList(SLL<Integer> list, int m, int n) {
- int sum = 0;
- SLLNode<Integer> temp = list.getFirst();
- for (int i = 0; i <= n; i++) {
- if (i >= m)
- sum += temp.element;
- temp = temp.succ;
- }
- SLLNode<Integer> prev = null;
- temp = list.getFirst();
- for (int i = 0; i < list.length(); i++) {
- if (i % 2 == 1) { // if odd
- SLLNode<Integer> newNode =
- new SLLNode<>(sum, temp.succ);
- prev.succ = newNode;
- }
- prev = temp;
- temp = prev.succ;
- }
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int count = in.nextInt();
- SLL<Integer> list = new SLL<>();
- for (int i = 0; i < count; i++) {
- list.insertLast(in.nextInt());
- }
- int
- m = in.nextInt(),
- n = in.nextInt();
- transformList(list, m, n);
- System.out.println(list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement