A single List Node or a single List? - list

* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
}
};
In this leetcode problem,we are passing a single list node or a list as arguments in the function as l1 and l2?
Or How this leetcode structure works?

Related

Line 28: Char 5: error: expected member name or ';' after declaration specifiers return mergeTwoLists; ^ 1 error generated

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if(list1==NULL)
return list2;
if(list2==NULL)
return list1;
if(list1->next<=list2->next)
mergeTwoLists(list1->next,list2);
if(list1->next>=list2->next)
mergeTwoLists(list1,list2->next);
}
return mergeTwoLists;
};

Can you please explain me the following lines in below linkedlist C++ code:

Can you please explain me the meaning of following lines in below linked list code and if you know any alternate method to these lines then please share it as well.
ListNode *l3=NULL;
ListNode **node=&l3;
(*node)=new ListNode(sum%10);
node=&((*node)->next);
I have also marked them in the code as query 1, query2, query3 and query 4 for your convinience
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int sum=0;
ListNode *l3=NULL; //----------------------------------Query 1
ListNode **node=&l3;//---------------------------------Query 2
while(l1!=NULL||l2!=NULL||sum>0)
{
if(l1!=NULL)
{
sum+=l1->val;
l1=l1->next;
}
if(l2!=NULL)
{
sum+=l2->val;
l2=l2->next;
}
(*node)=new ListNode(sum%10);//--------------------Query 3
sum/=10;
node=&((*node)->next);//---------------------------Query 4
}
return l3;
}
};
ListNode *l3=NULL;
Declares a variable named l3 of type ListNode*, and initializes it to NULL.
ListNode **node=&l3;
Declares a variable named node of type ListNode**, and initializes it to point at the memory address of l3.
(*node)=new ListNode(sum%10);
Dereferences node to access the current ListNode* it is pointing at, and then sets that pointer to point at a new ListNode object constructed with sum%10 for its data.
node=&((*node)->next);
Sets node to point at the memory address of the new ListNode's next member.
This double-pointer trick is commonly used to keep track of the next ListNode* pointer that needs to be updated on each loop iteration, to avoid having to use an if statement and an extra variable, eg:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
...
ListNode *l3 = NULL, *last = NULL, *node;
while (...)
{
...
node = new ListNode(...);
...
if (l3 == NULL)
l3 = node;
else
last->next = node;
last = node;
}
return l3;
}

Is there any way to delete an element from the BST without using recursion and without using a second function?

I was going through a problem to delete an element from the BST, and I wrote the following program:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* deleteNode(TreeNode* r, int k) {
if(r==0)
return 0;
if(r->val<k)
{
r->right=deleteNode(r->right,k);
return r;
}
else if(r->val>k)
{
r->left=deleteNode(r->left,k);
return r;
}
if(r->left==0)
return r->right;
if(r->right==0)
return r->left;
TreeNode *p=r->right;
while(p->left)
p=p->left;
p->left=r->left;
return r->right;
}
};
The main problem that I want to solve is to include the last while loop as the part of recursion.
Note that I do not want to use any other function, like isMin(), etc.

leetcode inorder binary search tree traversal

I'm working on a leetcode problem: Given the root of a binary tree, return the inorder traversal of its nodes' values. I was thinking I can push the right, root and left value on a stack and then pop them to put them to a vector to revert the order. However, I am getting this error:() line 22 is the vector.push_back(root->right->val). I'm not sure why I am getting that
Line 22: Char 43: runtime error: member access within null pointer of type 'TreeNode' (solution.cpp)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:31:43
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
std::stack<TreeNode*> stack;
std::vector<int> vector;
stack.push(root->left);
while(!stack.empty()){
root->right = stack.top();
stack.pop();
vector.push_back(root->right->val);
if(root){
stack.push(root);
}
if(root->left){
stack.push(root->left);
}
}
return vector;
}
};

I keep getting constant output for my depth function of a tree

I keep getting the initialized value of max as my output for some reason
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
void maxi(struct TreeNode* A,int t,int k){
if(A->left==NULL&&A->right==NULL&&t>k) k=t;
if(A->left!=NULL) maxi(A->left,t+1,k);
if(A->right!=NULL) maxi(A->right,t+1,k);
}
int Solution::maxDepth(TreeNode* A) {
int l=0;
maxi(A,1,l);
return l;
}
The function keeps outputting initialized value of l. In this case 0.