Interview Question: Merge two sorted singly linked lists without creating new nodes

后端 未结 26 2970
有刺的猬
有刺的猬 2020-12-02 04:09

This is a programming question asked during a written test for an interview. \"You have two singly linked lists that are already sorted, you have to merge them and return a

26条回答
  •  萌比男神i
    2020-12-02 04:32

    Here is the algorithm on how to merge two sorted linked lists A and B:

    while A not empty or B not empty:
       if first element of A < first element of B:
          remove first element from A
          insert element into C
       end if
       else:
          remove first element from B
          insert element into C
    end while
    

    Here C will be the output list.

提交回复
热议问题