Thursday, August 27, 2015

Merge Two Sorted Lists

Problem Description
Given 2 Sorted LinkedList, merge them and return a new Sorted LinkedList.
Solution
This problem is a typical operation used in Merge Sort algorithm.
I. Python
  1. # Definition for singly-linked list.  
  2. # class ListNode(object):  
  3. #     def __init__(self, x):  
  4. #         self.val = x  
  5. #         self.next = None  
  6.   
  7. class Solution(object):  
  8.     def mergeTwoLists(self, l1, l2):  
  9.         """ 
  10.         :type l1: ListNode 
  11.         :type l2: ListNode 
  12.         :rtype: ListNode 
  13.         """  
  14.         n = ListNode(0)  
  15.         current = n  
  16.         while l1 and l2:  
  17.             if l1.val < l2.val:   
  18.                 current.next = l1  
  19.                 l1 = l1.next  
  20.             else:  
  21.                 current.next = l2  
  22.                 l2 = l2.next  
  23.             current = current.next  
  24.               
  25.         while l1:  
  26.             current.next = l1  
  27.             l1 = l1.next  
  28.             current = current.next  
  29.               
  30.         while l2:  
  31.             current.next = l2  
  32.             l2 = l2.next  
  33.             current = current.next  
  34.               
  35.         return n.next  
II. Java
  1. /** 
  2.  * Definition for singly-linked list. 
  3.  * public class ListNode { 
  4.  *     int val; 
  5.  *     ListNode next; 
  6.  *     ListNode(int x) { val = x; } 
  7.  * } 
  8.  */  
  9. public class Solution {  
  10.     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {  
  11.         ListNode n = new ListNode(0);  
  12.         ListNode current = n;  
  13.         while(l1 != null && l2 != null){  
  14.             if (l1.val < l2.val){  
  15.                 current.next = l1;  
  16.                 l1 = l1.next;  
  17.             }else{  
  18.                 current.next = l2;  
  19.                 l2 = l2.next;  
  20.             }  
  21.             current = current.next;  
  22.         }  
  23.         while (l1 != null){  
  24.             current.next = l1;  
  25.             l1 = l1.next;   
  26.             current = current.next;  
  27.         }  
  28.         while (l2 != null){  
  29.             current.next = l2;  
  30.             l2 = l2.next;  
  31.             current = current.next;  
  32.         }  
  33.         return n.next;  
  34.     }  
  35. }  
III. C++
  1. /** 
  2.  * Definition for singly-linked list. 
  3.  * struct ListNode { 
  4.  *     int val; 
  5.  *     ListNode *next; 
  6.  *     ListNode(int x) : val(x), next(NULL) {} 
  7.  * }; 
  8.  */  
  9. class Solution {  
  10. public:  
  11.     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {  
  12.         ListNode* n = new ListNode(0);  
  13.         ListNode* current = n;  
  14.         while (l1 != NULL && l2!= NULL){  
  15.             if (l1->val < l2->val){  
  16.                 current->next = l1;  
  17.                 l1 = l1->next;  
  18.             }else{  
  19.                 current->next = l2;  
  20.                 l2 = l2->next;  
  21.             }  
  22.             current = current->next;  
  23.         }  
  24.         while(l1 != NULL){  
  25.             current->next = l1;  
  26.             l1 = l1->next;  
  27.             current = current->next;  
  28.         }  
  29.         while(l2 != NULL){  
  30.             current->next = l2;  
  31.             l2 = l2->next;  
  32.             current = current->next;  
  33.         }  
  34.         return n->next;  
  35.     }  
  36. };  
IV. Javascript
  1. /** 
  2.  * Definition for singly-linked list. 
  3.  * function ListNode(val) { 
  4.  *     this.val = val; 
  5.  *     this.next = null; 
  6.  * } 
  7.  */  
  8. /** 
  9.  * @param {ListNode} l1 
  10.  * @param {ListNode} l2 
  11.  * @return {ListNode} 
  12.  */  
  13. var mergeTwoLists = function(l1, l2) {  
  14.     var n = new ListNode(0);  
  15.     var current = n;  
  16.     while(l1 && l2){  
  17.         if(l1.val < l2.val) {  
  18.             current.next = l1;  
  19.             l1 = l1.next;  
  20.         }else{  
  21.             current.next = l2;  
  22.             l2 = l2.next;  
  23.         }  
  24.         current = current.next;  
  25.     }  
  26.     while(l1){  
  27.         current.next = l1;  
  28.         l1 = l1.next;  
  29.         current = current.next;  
  30.     }  
  31.     while(l2){  
  32.         current.next = l2;  
  33.         l2 = l2.next;  
  34.         current = current.next;  
  35.     }  
  36.     return n.next;  
  37. };  

No comments:

Post a Comment