Find and Delete the N-th Element from End of a Linked List (Java)

Implement a function that finds the nth node in a linked list, counting from the end.

Examples:

head = 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> (null / None)

The third element of head counting from the end is 3.

head2 = 1 -> 2 -> 3 -> 4 -> (null / None)

The fourth element of head2 counting from the end is 1.

If the given n is larger than the number of nodes in the list, return null / None.

 

Solution 1 (two passes):

Find the link list length first using a function list_len(Node head),  then calculate how many positions needs to be moved to the right from the head node.

public static Node nthFromLast(Node head, int n) {
    int len = list_len(head);
    System.out.println(len);
    Node cur = head;
 
    if(n>len || len<=0){
       return null;
     }
 
    while(len>n){
       len--;
       cur=cur.child;
     }
 
   return cur;
 }
 
public static int list_len(Node head){
    int count=0;
    Node cur = head;
 
    while(cur!=null){
        count++;
        cur= cur.child;
     }
 
    return count;
 }

This above solution need to iterate the link list twice, the worst running time is O(2n), n is the number of elements in the link list

Solution 2 (one pass):

Create 2 pointers p1 and p2, p2 is the nth position to the right of p1. move p1 and p2 together until p2 reaches the last null, then p1 points to the expected element.

 public static Node nthFromLast(Node head, int n) {
     Node p1 = head;
     Node p2 = head;
     int shift =n;
 
     if (p2==null){
         return null;
     }
 
 // shift p2 n positions to the right
     while(shift>0){
         if (p2==null){
         return null;
         }
     shift--;
     p2=p2.child;
     }
 
 //shift p2 and p1 together untill p2 reaches null
     while(p2!=null){
         p1=p1.child;
         p2=p2.child;
         }
 
     return p1;
 }

This above solution needs to iterate the link list only once, the worst running time is O(n), n is the number of elements in the link list.

To delete the found Nth Node from the End of the Linked List:

Given a linked list, remove the nth node from the end of a 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.

We know how to find p1. To delete p1, we connect p1’s previous node to p1’s next node and we must keep track of p1’s previous node. Because given n is always valid, there is one corner case we must consider: After shifting p2, now p1 points to the head and p2 points to null:

1->2->3->Null (n = 3)
1->2->Null    (n = 2)
1->Null       (n = 1)

In above case, we just return head.next;

For other cases, we can just shift p2 one more position right, so that when p2 shifts to null, p1 is pointing to the previous node of the one needs to be deleted. The answer is accepted by Leetcode.

 public static ListNode removeNthFromEnd(ListNode head, int n) {
     ListNode p1 = head;
     ListNode p2 = head;
     int count = n;
 
     while (count>0){
         p2 = p2.next;
         count--; 
     }
 
     // corner case. 
     if (p2==null){
         return head.next;
     }
 
     // shift p2 one more position
     p2=p2.next;
 
     while (p2!=null){
         p1=p1.next;
         p2=p2.next;
     }
 
     p1.next = p1.next.next;
 
     return head;
 
 }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s