Wednesday 6 July 2016

Hackerrank , 30 Days of Code Challenges ( Day 24 & 25 Solutions)

Day 24: More Linked Lists   :

public static Node removeDuplicates(Node head) {
     //Write your code here
       Node currentNode = head;
       while(currentNode!=null && currentNode.next!=null)
           {
           Node node = currentNode;
           while(node.next!=null)
               {
               if(node.next.data==currentNode.data)
                   {
                   Node next = node.next.next;
                   Node temp= node.next;
                   node.next=next;
                   temp=null;

               }
               else{
               node=node.next;
               }
           }
           currentNode=currentNode.next;
       }
       return head;
   }

Day 25: Running Time and Complexity  :

# Enter your code here. Read input from STDIN. Print output to STDOUT
import math
t=input()
def isPrime(data):
    if data < 2:
        return False
    v=int(math.sqrt(data))
    for i in range(2,v+1):
        if data%i==0:
            return False;
    return True;
for i in range(t):
    if isPrime(input()):
        print "Prime"
    else:
        print "Not prime"

   

No comments:

Post a Comment