Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SumNumbersSingleLinkedList {
- public static class SLLNode<Integer> {
- protected int element;
- protected SLLNode<Integer> succ;
- public SLLNode (int elem, SLLNode<Integer> succ) {
- this.element = elem;
- this.succ = succ;
- }
- public void insertAfter(int o, SLLNode<Integer> node){
- if (node != null){
- SLLNode<Integer> ins = new SLLNode<Integer>(o, node.succ);
- node.succ = ins;
- }
- }
- public int getElement()
- {
- return element;
- }
- }
- public static class SLL<Integer>{
- private SLLNode<Integer> first;
- public SLL (){
- this.first = null;
- }
- public void insertFirst(int o){
- SLLNode<Integer> ins = new SLLNode<Integer>(o, first);
- first = ins;
- }
- public SLLNode<Integer> getFirst()
- {
- return this.first;
- }
- }
- public static void main(String[] args) {
- Scanner Scanner=new Scanner(System.in);
- int N,i,j;
- SLL<Integer> nova = new SLL();
- SLLNode<Integer> temp= new SLLNode(0,nova.getFirst());
- SLLNode<Integer> temp1= new SLLNode(0,nova.getFirst());
- N=Scanner.nextInt();
- for(i=0;i<N;i++){
- j=Scanner.nextInt();
- if(i==0)
- {
- nova.insertFirst(j);
- temp = nova.getFirst();
- }
- else
- {
- temp1.insertAfter(j, temp);
- temp=temp.succ;
- // temp.succ.succ = null;
- }
- }
- int suma=0;
- temp.succ=nova.getFirst();
- //System.out.print(temp.succ.getElement());
- for(i =0;i<N;i++)
- {
- suma+=temp.succ.getElement();
- if(temp.succ!=null)
- temp= temp.succ;
- }
- System.out.print(suma);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement