www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Interview Question Remove Nth Node From End of List

Last update: 1/2/2017 9:04:00 PM

By Fatih KABAKCI

The problem is to be able to delete nth node from end of the list in a given linked list by one passing at a time. Given n will always be valid. So, there is no need to check that out.

Problem #19 - Remove Nth Node From End of List

Description: Given a linked list, remove the nth node from the end of list and return its head.

Difficulty: Easy

Solution in Java

package com.fatihkabakci.Easy.RemoveNthNodeFromEndofList;

/**
 * @author fkabakci
 * 
 * Problem Description: Given a linked list, remove the nth node from the end of list and return its head.
 * 
 * For example,
 * 
 * Given linked list: 1->2->3->4->5, and n = 2.
 * After removing the second node from the end, the linked list becomes 1->2->3->5.
 * 
 * Note: Given n will always be valid. Try to do this in one pass.
 * 
 * Solution:
 * Purpose is to fall down the pointer - slow in front of the node which needs to be deleted.
 * So then, the final part slow.next = slow.next.next; is going to be able to work properly.
 * 
 * 1. Move the pointer - fast to n + 1 forward.
 * 	  if fast is already null during this process
 *       Means that first data demands to be deleted because n is always valid.
 * 2. Move both the pointers fast and slow to forward until fast is null.
 * 3. Connect the apart parts to each other by dropping the node which is wanted to delete.
 * 
 *  */
public class RemoveNthNodeFromEndofList {	
	public ListNode removeNthFromEnd(ListNode head, int n) {
		ListNode fast = head, slow = head;
		for (int i = 1; i <= n + 1; i++) {
			// for the first element to be able to deleted.
			if (fast == null)
				return head.next;
			fast = fast.next;
		}

		while (fast != null) {
			fast = fast.next;
			slow = slow.next;
		}

		slow.next = slow.next.next;
		return head;
    }
	
	public static void main(String[] args) {
		RemoveNthNodeFromEndofList r = new RemoveNthNodeFromEndofList();
		ListNode head = Data.init(new int[]{1, 2, 3});
		ListNode remain = r.removeNthFromEnd(head, 1);
		while(remain != null) {
			System.out.print(remain.val + " ->");
			remain = remain.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;
	}
}

1/2/2017 10:11:00 AM

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,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.