Skip to content

Instantly share code, notes, and snippets.

@kmurudi
Created October 9, 2017 03:48
Show Gist options
  • Select an option

  • Save kmurudi/c519b06713bc9731b1e964d54f418953 to your computer and use it in GitHub Desktop.

Select an option

Save kmurudi/c519b06713bc9731b1e964d54f418953 to your computer and use it in GitHub Desktop.
class Node{
int data;
Node next;
Node(int k){
data = k;
next=null;
}
}
public class SortLL {
Node head;
Node compare(Node n, Node new1){
Node d1 = n;
Node d2 = n.next;
if(new1.data>n.data){
return n.next;
}
else
return n;
}
public void addInOrder(int key){
Node current;
Node new1 = new Node(key);
if(head==null || head.data>=key){
new1.next = head;
head = new1;
}
else{
current = head;
while(current.next!=null && current.next.data<key){
current = current.next;
}
new1.next = current.next;
current.next=new1;
}
}
public static void main(String args[]){
SortLL l1 = new SortLL();
l1.head = new Node(1);
Node two = new Node(2);
Node three = new Node(8);
l1.head.next = two;
two.next = three;
l1.addInOrder(4);
Node temp = l1.head;
while(temp!=null){
System.out.println(temp.data);
temp=temp.next;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment