Why is this merge sort function returning linked list with zeroes (c++)?
By : P Jarrell
Date : March 29 2020, 07:55 AM
it should still fix some issue After checking your full code in the link you provided, I can definitely say the problem is because you don't have an assignment operator. What happens now is that the assignments of the lists will use the default assignment operator that is automatically generated by the compiler. This does a shallow copy, so the list on the left hand side of the assignment will have its pointers be the same as for the list on the right hand side. This means that when the local variable you return goes out of scope, it will of course invoke the destructor which deletes the lists. Now the copy have pointers which points to deleted memory, and accessing thos pointers is undefined behavior. This is why it seems to work in one place and not the other.

Merge sort two linked list into a third linked list via recursion  c++
By : Krishna Londhe Beed
Date : March 29 2020, 07:55 AM
it fixes the issue update  you need to advance headX or headY during the merge sort. Also the empty lists checks can be simplfied. This example seems to work: code :
void SortedMergeRecur(Node* &headX, Node* &headY, Node* &headZ)
{
// assume that both headX and headY are sorted linkedlists
// check for empty lists
if(headX == 0)
{
headZ = headY;
headY = 0;
return;
}
if(headY == 0)
{
headZ = headX;
headX = 0;
return;
}
// move smaller of X,Y to Z
if (headX>data <= headY>data)
{
headZ = headX;
headX = headX>link;
SortedMergeRecur(headX, headY, headZ>link);
}
else
{
headZ = headY;
headY = headY>link;
SortedMergeRecur(headX, headY, headZ>link);
}
return;
}

Not able to merge Sorted Linked Lists using Auxiliary Linked List
By : Robatson
Date : March 29 2020, 07:55 AM
it fixes the issue Please give a simple solution to problem. I have used the algorithm like mergesort but I am not able return head of the auxiliary Linked list, I created. I have seen other example on stack overflow. But I want to know where is the problem with my code. code :
/**
* Definition for singlylinked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* Solution::mergeTwoLists(ListNode* A, ListNode* B) {
if (A == NULL){
return B;
}
if (B == NULL){
return A;
}
ListNode* head;
ListNode* root = new ListNode(0); //initialized root
ListNode* H1 = A;
ListNode* H2 = B;
if(H1>val < H2>val){
root>val = H1>val;
head = root;
H1 = H1>next;
}else{
root>val = H2>val;
head = root;
H2 = H2>next;
}
while (H1 != NULL && H2 != NULL){
if(H1>val < H2>val){
root>next = new ListNode(H1>val);
root = root>next; //making list
H1 = H1>next;
}else{
root>next = new ListNode(H2>val);
root = root>next;
H2 = H2>next;
}
}
while (H2 != NULL){
root>next = new ListNode(H2>val);
root = root>next;
H2 = H2>next;
}
while (H1 != NULL){
root>next = new ListNode(H1>val);
root = root>next;
H1 = H1>next;
}
return head;
}

Recursive Alternative/Merge Linked List Function with Multiple Linked List as Arguments?
By : Phil Rombs
Date : March 29 2020, 07:55 AM
it fixes the issue You should use classes instead of simple functions as helpers. And accept any iterable as the source for a linked list. If you implement iterators on the linked list class, that would allow trivial conversion between any iterable and a linked list. The linked list class could be: code :
class LL:
class iter:
def __init__(self, ll):
self.cur = ll.front
def __iter__(self):
return self
def __next__(self):
if self.cur is None:
raise StopIteration()
val = self.cur.value
self.cur = self.cur.next
return val
def __init__(self, l):
self.front = last = None
for v in l:
ln = LN(v)
if last is None:
self.front = ln
else:
last.next = ln
last = ln
def __str__(self):
answer = ''
for val in self.iter_elt():
answer += str(val) + '>'
return answer + 'None'
def __repr__(self):
return str(self.__class__) + ':' + str(self)
def __iter__(self):
return LL.iter(self)
>>> print(LL('abcd'))
a>b>c>d>None
>>> list(LL('abcd'))
['a', 'b', 'c', 'd']
class LL:
...
def __str__(self):
answer = ''
for val in self.iter_elt():
answer += str(val) + '>'
return answer + 'None'
...
def iter_elt(self):
return self.__iter__()
class RLL(LL):
class iter:
def __init__(self, rll):
self.iters = LL(i.iter_elt() if hasattr(i, 'iter_elt') else iter(i)
for i in rll)
self.cur = self.iters.front
self.prev = None
def __iter__(self):
return self
def __next__(self):
try:
elt = next(self.cur.value)
self.prev = self.cur
self.cur = self.cur.next
if self.cur is None:
self.cur = self.iters.front
self.prev = None
except StopIteration:
self.cur = self.cur.next
if self.cur is None:
if self.prev is None:
raise
self.cur = self.iters.front
self.prev = None
else:
if self.prev is None:
self.iters.front = self.cur
else:
self.prev.next = self.cur
elt = self.__next__()
return elt
def iter_elt(self):
return RLL.iter(self)

Javascript: Linked object/list function returning undefined
By : user3126526
Date : March 29 2020, 07:55 AM
should help you out I am trying to return the length of a linked object(list). However, the function I wrote doesn't return anything. , You need to return the recursive call: code :
return countDepth(liste, count);
const countDepth = (l, c = 0) => !l ? c : countDepth(l.rest, ++c);

