Created
July 17, 2021 21:22
-
-
Save wohhie/ce3981dfa8086b5820cb707b728a7243 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
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