Join us on the demo, while our product experts provide a detailed walkthrough of our enterprise platform.

logoAsync

The latest news in the world of engineering.

Identity and Access Management (IAM), including security and customer experience.

Visit Blog
Fuel

Grow your business to millions.Engage and retain your customers.

Visit Blog

Delete a Node, Is Same Tree, Move Zeroes

Delete A Node Click to View the Orginal Question To remove the given node,which means whoever is asking the value for this node should get the value of the next…

Lucius Yu
Lucius Yu
December 22, 2015
2 min read

Free, Secure and Trusted Way to Authenticate Your Visitors

Add login to your website in 5 minutes completely for free!

Free Sign UpNo hidden costs. No credit card needed.

Delete A Node

Click to View the Orginal Question

To remove the given node,which means whoever is asking the value for this node should get the value of the next node. Plus, the next node of this removing one should be the next next node.

In diagram, with the following node list, we want to remove node 2.

1 -> 2 -> 3 -> 4

We update original Node 2 to Node 3 and skip the original Node 3, which gives out the list after removing node 2.

1 -> 3 ------> 4

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} node
* @return {void} Do not return anything, modify node in-place instead.
*/
var deleteNode = function(node) {
    node.val = node.next.val;
    node.next = node.next.next;
};

Is Same Tree

Click to View the Original Question

Note: My solution is not efficient enough based on the performance, but I think it's fairly clean.

I use recursion to solve the problem, first to define the basic cases, and recursively loop through both trees from left to right. If it hits one false, the whole result will be false.

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
 
var isSameTree = function(p, q) {
    if( p === q ) return true;
    if(!p || !q || p.val !== q.val) return false;
    return isSameTree(p.left, q.left) ? isSameTree(p.right, q.right) : false;
};

Move Zeroes

Click to View the Original Question

Here I have create two indices one called z to index the position for zeroes, another is i to loop through the nums array. They both increment by one when they see a non-zero value, but when it is zero, z stays at position and get ready to swap the position with next non-zero value i meets. By keep doing it, it will fill a consecutive row of zeroes, till the end.

<!--/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
    var z = 0;
    for(var i = 0; i 


LoginRadius Docs

Implement Authentication in Minutes

click here

Most Popular Tags

EngineeringJavaScriptReactNodeJsAuthenticationCSSSecurityC#OAuthGo

Do you want a free authentication solution?

Add the world's most secure, reliable and easy to implement user authentication solution on your applications at $0Get Started Free

Lucius Yu

Lucius Yu

Lucius is a Software Developer at LoginRadius. He was born and raised in China, but came to Edmonton to graduate from the University of Alberta, double majoring in Computer Sciences and Mathematics. Besides coding, which is his real passion, he enjoys playing ping pong and swimming.

View Profile

Try a Modern Authentication Solution

$0/ month

Free Sign Up
  • 7,000 Monthly Active Users
  • 1 Web or Mobile App
  • Email/Password Login
  • 3 Social Login Providers
  • User Data Storage
  • Transactional Email Template
  • Customizable Login Interfaces