java - Merge two sorted linklist recursively? -


i cant find wrong code when submit 2 of testcases giving runtime error . please me figure out error. have checked atleast 30 custom testcases gives right output of them.

code

public static node mergetwolist(node head1, node head2) {     node c = null;             if (head1 == null) {         return head2;     } else if (head2 == null) {         return head1;     }      if (head1.data < head2.data) {         c = head1;         c.next = mergetwolist(head1.next, head2);     } else {         c = head2;         c.next = mergetwolist(head1, head2.next);      }     return c; } 

if figure out please tell me.

i think reason stackoverflow, because use recursion, , recursion produce stack, , if linklist long, may cause stackoverflow.

there similar question on leecode, , solve iterative, paste solution in blog, although explanation in madarin, code can still reference. link provided below: http://codecrazer.blogspot.tw/2017/07/leetcode-21-merge-two-sorted-lists.html


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -