📌  相关文章
📜  从链接列表的末尾删除第N个节点

📅  最后修改于: 2021-04-29 13:51:52             🧑  作者: Mango

给定一个链表。任务是从链接列表的末尾删除第N个节点。

例子:

先决条件:
1.从链接列表中删除一个节点。
2.从链接列表的末尾找到第n个节点

方法:
从最后删除第B个节点与从开始删除(length-B + 1)基本相同。在我们的方法中,我们首先评估链表的长度,然后检查

  • 如果长度
  • 如果length = B,则返回head-> next
  • 如果长度> B,则意味着我们必须删除中间节点,我们将删除此节点,并使它的上一个节点指向已删除节点的下一个节点。
C++
// CPP program to delete nth node from last
#include 
using namespace std;
  
// Structure of node
struct Node {
    int data;
    struct Node* next;
};
  
// Function to insert node in a linked list
struct Node* create(struct Node* head, int x)
{
    struct Node *temp, *ptr = head;
    temp = new Node();
    temp->data = x;
    temp->next = NULL;
    if (head == NULL)
        head = temp;
    else {
        while (ptr->next != NULL) {
            ptr = ptr->next;
        }
        ptr->next = temp;
    }
    return head;
}
  
// Function to remove nth node from last
Node* removeNthFromEnd(Node* head, int B)
{
    // To store length of the linked list
    int len = 0;
    Node* tmp = head;
    while (tmp != NULL) {
        len++;
        tmp = tmp->next; 
    }
      
    // B > length, then we can't remove node
    if (B > len) 
    {
        cout << "Length of the linked list is " << len;
        cout  << " we can't remove "<< B << "th node from the";
        cout << " linked list\n";
        return head; 
    }
      
    // We need to remove head node
    else if (B == len) {
          
        // Return head->next
        return head->next; 
          
    }
    else 
    {
        // Remove len - B th node from starting
        int diff = len - B;          
        Node* prev= NULL;       
        Node* curr = head;         
        for(int i = 0;i < diff;i++){
            prev = curr;            
            curr = curr->next;      
        }
        prev->next = curr->next;
        return head;
    }
      
}
  
// This function prints contents of linked 
// list starting from the given node 
void dispaly(struct Node* head)
{
  
    struct Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}
  
// Driver code
int main()
{
    struct Node* head = NULL;
      
      
    head = create(head, 1);
    head = create(head, 2);
    head = create(head, 3);
    head = create(head, 4);
    head = create(head, 5);
      
    int n = 2;
      
    cout << "Linked list before modification: \n";
    dispaly(head);
  
    head = removeNthFromEnd(head, 2);
    cout << "Linked list after modification: \n";
    dispaly(head);
  
    return 0;
}


Java
// Java program to delete nth node from last
class GFG 
{ 
  
// Structure of node
static class Node
{
    int data;
    Node next;
};
  
// Function to insert node in a linked list
static Node create(Node head, int x)
{
    Node temp, ptr = head;
    temp = new Node();
    temp.data = x;
    temp.next = null;
    if (head == null)
        head = temp;
    else 
    {
        while (ptr.next != null) 
        {
            ptr = ptr.next;
        }
        ptr.next = temp;
    }
    return head;
}
  
// Function to remove nth node from last
static Node removeNthFromEnd(Node head, int B)
{
    // To store length of the linked list
    int len = 0;
    Node tmp = head;
    while (tmp != null) 
    {
        len++;
        tmp = tmp.next; 
    }
      
    // B > length, then we can't remove node
    if (B > len) 
    {
        System.out.print("Length of the linked list is " + len);
        System.out.print(" we can't remove "+ B + 
                         "th node from the");
        System.out.print(" linked list\n");
        return head; 
    }
      
    // We need to remove head node
    else if (B == len) 
    {
          
        // Return head.next
        return head.next; 
          
    }
    else
    {
        // Remove len - B th node from starting
        int diff = len - B;         
        Node prev= null;     
        Node curr = head;         
        for(int i = 0; i < diff; i++)
        {
            prev = curr;         
            curr = curr.next;     
        }
        prev.next = curr.next;
        return head;
    }
      
}
  
// This function prints contents of linked 
// list starting from the given node 
static void dispaly(Node head)
{
    Node temp = head;
    while (temp != null) 
    {
        System.out.print(temp.data + " ");
        temp = temp.next;
    }
    System.out.println();
}
  
// Driver code
public static void main(String[] args)
{
    Node head = null;
      
    head = create(head, 1);
    head = create(head, 2);
    head = create(head, 3);
    head = create(head, 4);
    head = create(head, 5);
      
    int n = 2;
      
    System.out.print("Linked list before modification: \n");
    dispaly(head);
  
    head = removeNthFromEnd(head, 2);
    System.out.print("Linked list after modification: \n");
    dispaly(head);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to delete nth node from last
class Node:
  
    # Function to initialise the node object
    def __init__(self, data):
        self.data = data # Assign data
        self.next = None # Initialize next as null
  
class LinkedList:
  
    # Function to initialize head
    def __init__(self):
        self.head = None
  
    # Function to add node at the end
    def create(self, x):
  
        new_node = Node(x)
  
        if self.head is None:
            self.head = new_node
            return
  
        last = self.head
        while last.next:
            last = last.next
  
        last.next = new_node
  
    # This function prints contents of linked 
    # list starting from the given node 
    def display(self):
        temp = self.head
  
        while temp:
            print(temp.data, end = " ")
            temp = temp.next
  
# Function to remove nth node from last
def removeNthFromEnd(head, k):
      
    # the function uses two pointer method
    first = head
    second = head
    count = 1
    while count <= k:
        second = second.next
        count += 1
    if second is None:
        head.value = head.next.value
        head.next = head.next.next
        return
    while second.next is not None:
        first = first.next
        second = second.next
    first.next = first.next.next
  
# Driver code
  
# val list contains list of values
val = [1, 2, 3, 4, 5]
k = 2
ll = LinkedList()
for i in val:
    ll.create(i)
  
print("Linked list before modification:");
ll.display()
  
removeNthFromEnd(ll.head, k)
  
print("\nLinked list after modification:");
ll.display()
  
# This code is contributed by Dhinesh


C#
// C# program to delete nth node from last
using System;
      
class GFG 
{ 
  
// Structure of node
class Node
{
    public int data;
    public Node next;
};
  
// Function to insert node in a linked list
static Node create(Node head, int x)
{
    Node temp, ptr = head;
    temp = new Node();
    temp.data = x;
    temp.next = null;
    if (head == null)
        head = temp;
    else
    {
        while (ptr.next != null) 
        {
            ptr = ptr.next;
        }
        ptr.next = temp;
    }
    return head;
}
  
// Function to remove nth node from last
static Node removeNthFromEnd(Node head, int B)
{
    // To store length of the linked list
    int len = 0;
    Node tmp = head;
    while (tmp != null) 
    {
        len++;
        tmp = tmp.next; 
    }
      
    // B > length, then we can't remove node
    if (B > len) 
    {
        Console.Write("Length of the linked list is " + len);
        Console.Write(" we can't remove " + B + 
                           "th node from the");
        Console.Write(" linked list\n");
        return head; 
    }
      
    // We need to remove head node
    else if (B == len) 
    {
          
        // Return head.next
        return head.next; 
          
    }
    else
    {
        // Remove len - B th node from starting
        int diff = len - B;         
        Node prev= null;     
        Node curr = head;         
        for(int i = 0; i < diff; i++)
        {
            prev = curr;         
            curr = curr.next;     
        }
        prev.next = curr.next;
        return head;
    }
      
}
  
// This function prints contents of linked 
// list starting from the given node 
static void dispaly(Node head)
{
    Node temp = head;
    while (temp != null) 
    {
        Console.Write(temp.data + " ");
        temp = temp.next;
    }
    Console.Write("\n");
}
  
// Driver code
public static void Main(String[] args)
{
    Node head = null;
      
    head = create(head, 1);
    head = create(head, 2);
    head = create(head, 3);
    head = create(head, 4);
    head = create(head, 5);
      
    Console.Write("Linked list before modification: \n");
    dispaly(head);
  
    head = removeNthFromEnd(head, 2);
    Console.Write("Linked list after modification: \n");
    dispaly(head);
}
}
  
// This code is contributed by 29AjayKumar


输出:

Linked list before modification: 
1 2 3 4 5 
Linked list after modification: 
1 2 3 5