Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

merge two sorted lists

var mergeTwoLists = function(list1, list2) {

    // using dummy head to avoid writing additional code for edge cases (e.g. list = null).
    // using "new" keyword to create a new object. It makes the this variable point to the newly created object.
	// p is the pointer of dummy, p1 is the pointer of list1, p2 is the pointer of list2
    let dummy = new ListNode(0);
    let p = dummy;
    let p1 = list1;
    let p2 = list2;

    //   while both lists are not null.
    while (p1 && p2 ){
	
        // compare the number of nodes. insert the smaller node to the result
        if (p1.val > p2.val){
            p.next = p2;
            p2 = p2.next;
        }else{
            p.next = p1;
            p1 = p1.next;
        }
        
        // move the pointer to the next one
        p = p.next;
    }
    
    // if every node of one list has been traversaled, we will insert the rest of the other list to the result
    p.next = p1 || p2;
    return dummy.next;
};
Comment

merge sort of two list in python

a=[2,4,1,4]
b=[0,2,3,4]
a.extend(b)
a.sort()
print(a)
Comment

Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        
    }
};
Comment

Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        
    }
}
Comment

Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){

}
Comment

Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode MergeTwoLists(ListNode list1, ListNode list2) {
        
    }
}
Comment

Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} list1
 * @param {ListNode} list2
 * @return {ListNode}
 */
var mergeTwoLists = function(list1, list2) {
    
};
Comment

Merge Two Sorted Lists

# Definition for singly-linked list.
# class ListNode
#     attr_accessor :val, :next
#     def initialize(val = 0, _next = nil)
#         @val = val
#         @next = _next
#     end
# end
# @param {ListNode} list1
# @param {ListNode} list2
# @return {ListNode}
def merge_two_lists(list1, list2)
    
end
Comment

Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init() { self.val = 0; self.next = nil; }
 *     public init(_ val: Int) { self.val = val; self.next = nil; }
 *     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
 * }
 */
class Solution {
    func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
        
    }
}
Comment

Merge Two Sorted Lists

/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val = 0, $next = null) {
 *         $this->val = $val;
 *         $this->next = $next;
 *     }
 * }
 */
class Solution {

    /**
     * @param ListNode $list1
     * @param ListNode $list2
     * @return ListNode
     */
    function mergeTwoLists($list1, $list2) {
        
    }
}
Comment

Merge Two Sorted Lists

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {

};
Comment

PREVIOUS NEXT
Code Example
Python :: iterrrows 
Python :: Combine integer in list 
Python :: sphinx autodoc extension 
Python :: save and load model during training pytorch 
Python :: chr() function in python 
Python :: django email 
Python :: Iterating Through Dictionaries with For Loops 
Python :: django model different schema 
Python :: linkedin api with python 
Python :: python machine learning 
Python :: faker, generates fake data for you 
Python :: python check if string or list 
Python :: lambda function if else in python 
Python :: negative indexing in python 
Python :: if df[col].unique()==2 
Python :: python integer to string format 
Python :: dictionary append value python 
Python :: why pytest return No ModuleError 
Python :: py virtual 
Python :: convert df.isnull().sum() to dataframe 
Python :: knuth morris pratt algorithm 
Python :: Python how to search in string 
Python :: for loop 
Python :: python - How to execute a program or call a system command? 
Python :: python import matplotlib 
Python :: black python 
Python :: 1*2*3*4*5*6* - print on console?by python 
Python :: python newline 
Python :: intersection of two lists using set method 
Python :: python jointly shuffle list 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =