Skip to content

Instantly share code, notes, and snippets.

@ugandapinik
Created July 17, 2021 21:22
Show Gist options
  • Save ugandapinik/ce3981dfa8086b5820cb707b728a7243 to your computer and use it in GitHub Desktop.
Save ugandapinik/ce3981dfa8086b5820cb707b728a7243 to your computer and use it in GitHub Desktop.
public Node insertData(Node llist, int data){
// Write your code here
Node curr = llist;
Node node = new Node(data);
if (llist == null) return node;
else if(node.data < curr.data){
// insert in the beginning
node.next = curr;
curr.prev = node;
return node;
}else{
// node = 3 curr = 1, 2, 4;
//
while(curr != null) {
if (curr.next == null || curr.next.data >= data){
node.next = curr.next;
node.prev = curr;
if (curr.next != null){
curr.next.prev = node;
}
curr.next = node;
break;
}
curr = curr.next;
}
}
return curr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment