Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RankingSolution {
- private Node startNode = null;
- public int getRank(int input) {
- int rank = 0;
- if (startNode == null) {
- startNode = new Node(input);
- return rank;
- }
- if (startNode.value > input) {
- Node newNode = new Node(input);
- newNode.next = startNode;
- startNode = newNode;
- return rank;
- }
- Node currNode = startNode;
- rank++;
- while (currNode.next != null) {
- Node nextNode = currNode.next;
- if (nextNode.value == input) {
- return rank;
- }
- if (nextNode.value > input) {
- Node newNode = new Node(input);
- newNode.next = nextNode;
- currNode.next = newNode;
- return rank;
- }
- currNode = currNode.next;
- rank++;
- }
- currNode.next = new Node(input);
- return rank;
- }
- class Node {
- int value;
- Node next = null;
- public Node(int value) {
- this.value = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement