Created
October 19, 2017 15:15
-
-
Save khayyamsaleem/15047a018ab7a678893d5f56a162188f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class Node { | |
int data; | |
Node next; | |
Node(int d){ | |
data = d; | |
next = null; | |
} | |
static Node insert(Node head, int data){ | |
Node p = new Node(data); | |
if(head == null){ | |
head = p; | |
}else if(head.next == null){ | |
head.next = p; | |
}else{ | |
Node start = head; | |
while(start.next != null){ | |
start = start.next; | |
} | |
start.next = p; | |
} | |
return head; | |
} | |
static void display(Node head){ | |
Node start = head; | |
while(start != null){ | |
System.out.print(start.data+" "); | |
start = start.next; | |
} | |
System.out.println(); | |
} | |
static Node removeDuplicates(Node head){ | |
if(head == null || head.next == null){ | |
return head; | |
} | |
Node start = head; | |
while(start.next != null){ | |
if (start.data == (start.next).data){ | |
start.next = (start.next).next; | |
}else{ | |
start = start.next; | |
} | |
} | |
return head; | |
} | |
public static void main(String args[]) | |
{ | |
Scanner sc=new Scanner(System.in); | |
Node head = null; | |
System.out.println("Enter a list size: "); | |
int T = sc.nextInt(); | |
while(T-- > 0){ | |
System.out.println("Enter a number: "); | |
int ele = sc.nextInt(); | |
head = insert(head, ele); | |
} | |
display(head); | |
head = removeDuplicates(head); | |
display(head); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment