www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Merge Two Sorted Lists

Last update: 1/7/2017 7:32:00 AM

By Fatih KABAKCI

The problem is to merge two sorted list into a new list.

Problem #21 - Merge Two Sorted Lists

Description: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.MergeTwoSortedLists;

/**
 * @author fkabakci
 * 
 * Problem Description: Merge two sorted linked lists and return it as a new list. 
 * The new list should be made by splicing together the nodes of the first two lists.
 * 
 * Solution:
 * 1. Start with the first elements. See that which one is lower than the another one, 
 * 	  then put that one (minimum one) into the list.
 * 2. Pass to the next one in the list which had a minimum one previous.
 */
public class MergeTwoSortedLists {	
	public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
		ListNode head = new ListNode(0);
        ListNode l3 = head;
        while(l1 != null || l2 != null) {
        	int val1 = (l1 != null) ? l1.val : Integer.MAX_VALUE;
        	int val2 = (l2 != null) ? l2.val : Integer.MAX_VALUE;
        	l3.next = new ListNode(Math.min(val1, val2));
    		l3 = l3.next;
        	if(val1 < val2) 
        		l1 = l1.next;
        	else
        		l2 = l2.next;
        }
        return head.next;
    }
	
	public static void main(String[] args) {
		MergeTwoSortedLists r = new MergeTwoSortedLists();
		ListNode head = Data.init(new int[]{1, 2, 3});
		ListNode tail = Data.init(new int[]{4, 5, 6});
		ListNode entire = r.mergeTwoLists(head, tail);
		while(entire != null) {
			System.out.print(entire.val + " ->");
			entire = entire.next;
		}
	}
}

MemberCommentDate:
Fatih KABAKCI
ListNode and the helper class Data which is used to initialize linked list are below.

public class ListNode {
	int val;
	ListNode next;
	ListNode(int x) {
		val = x;
	}
}

package com.fatihkabakci.Easy.RemoveNthNodeFromEndofList;

public class Data {
	public static ListNode init(int[] data) {
		ListNode head = new ListNode(data[0]);
		ListNode t = head;
		for(int i = 1; i < data.length; i++) {
			t.next = new ListNode(data[i]);
			t = t.next;
		}
		return head;
	}
}

6/1/2017 8:37:00 PM

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.