![]() ![]() Both prevNode and nextNode are pointed to null, and currNode is set to be the current head of the linked list.Ī while loop is begun with a termination condition of the currNode pointing to null, and the switching process begins: To accomplish this, we instantiate three nodes - prevNode, nextNode and currNode. Let's take a look at the iterative pseudo-code behind the implementation: Node prevNode, currNode, nextNode There are two ways to approach this problem: iterative and recursive. Reverse the given linked list: 1->2->3->4->5 Ultimately, if it passes all the checks, it means that the lists match and represent the same String, returning 0. If the content of the second node is greater than that of the first node, we return -1Īnd the final two if statements simply returns 1 and -1 accordingly if the list sizes don't match.If the content of the first node is greater than that of the second node, we return 1.The if statement check whether both of the nodes aren't null: If any of these two conditions return false, the loop breaks. This means that it will traverse both of the lists, node by node, as long as they don't reach the end of the list, and each character matches between them. The while loop traverses both lists with two conditions attached: While node and node are not null AND ntent is equal to ntent If implemented correctly, using that function on this particular example should produce the following result: -1 Note: "Lexicographically greater" means that a String would appear after the other String based on the dictionary position. -1 - If the second list is lexicographically greater than the first list.1 - If the first list is lexicographically greater than the second list.0 - If the two lists represent the same String.Inserting at the end of the linked list.Inserting at the start of the linked list.There are a few ways someone can ask you to do this: Let's start off with a simple task - inserting a node into a list. Insert an element (6) on the 2nd position in the following list: 2->4->5->3->7->0 Remove the 4th element from the following list: 2->4->5->3->7->0 ![]() Inserting and Removing Nodes Interview Question Need help studying for your interview? We recommend trying a service like Daily Coding Problem, which will email you practice questions daily.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |