Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. public class RankingSolution {
  2.     private Node startNode = null;
  3.  
  4.     public int getRank(int input) {
  5.         int rank = 0;
  6.  
  7.         if (startNode == null) {
  8.             startNode = new Node(input);
  9.             return rank;
  10.         }
  11.  
  12.         if (startNode.value > input) {
  13.             Node newNode = new Node(input);
  14.             newNode.next = startNode;
  15.             startNode = newNode;
  16.  
  17.             return rank;
  18.         }
  19.  
  20.         Node currNode = startNode;
  21.         rank++;
  22.  
  23.         while (currNode.next != null) {
  24.             Node nextNode = currNode.next;
  25.  
  26.             if (nextNode.value == input) {
  27.                 return rank;
  28.             }
  29.  
  30.             if (nextNode.value > input) {
  31.                 Node newNode = new Node(input);
  32.                 newNode.next = nextNode;
  33.                 currNode.next = newNode;
  34.  
  35.                 return rank;
  36.             }
  37.  
  38.             currNode = currNode.next;
  39.             rank++;
  40.         }
  41.  
  42.         currNode.next = new Node(input);
  43.  
  44.         return rank;
  45.     }
  46.  
  47.     class Node {
  48.         int value;
  49.         Node next = null;
  50.  
  51.         public Node(int value) {
  52.             this.value = value;
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement