shashwat2

merge two sorted linked list

Mar 5th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.27 KB | None | 0 0
  1. ListNode* merge(ListNode* p, ListNode* q) {
  2. if(!p) return q;
  3. if(!q) return p;
  4.  
  5. if(p->val < q->val)
  6. {
  7. p->next = merge(p->next, q);
  8. return p;
  9. }
  10. else
  11. {
  12. q->next = merge(p, q->next);
  13. return q;
  14. }
  15. }
Add Comment
Please, Sign In to add comment