📌  相关文章
📜  从链表末尾打印第 N 个节点的 C++ 程序

📅  最后修改于: 2022-05-13 01:55:07.753000             🧑  作者: Mango

从链表末尾打印第 N 个节点的 C++ 程序

给定一个链表和一个数字 n,编写一个函数,返回链表末尾第 n 个节点的值。
例如,如果输入低于列表且 n = 3,则输出为“B”

链表

方法一(使用链表长度):

  1. 计算链表的长度。设长度为 len。
  2. 从链表的开头打印第 (len – n + 1) 个节点。

双指针概念:第一个指针用来存放变量的地址,第二个指针用来存放第一个指针的地址。如果我们希望通过函数更改变量的值,我们将指针传递给它。如果我们希望改变一个指针的值(即,它应该开始指向别的东西),我们将指针传递给一个指针。

下面是上述方法的实现:

C++14
// C++ program to find n'th node from 
// end
#include 
using namespace std;
  
// Link list node 
struct Node 
{
    int data;
    struct Node* next;
};
  
/* Function to get the nth node from 
   the last of a linked list*/
void printNthFromLast(struct Node* head, 
                      int n)
{
    int len = 0, i;
    struct Node* temp = head;
  
    // count the number of nodes in 
    // Linked List
    while (temp != NULL) 
    {
        temp = temp->next;
        len++;
    }
  
    // check if value of n is not
    // more than length of the 
    // linked list
    if (len < n)
        return;
  
    temp = head;
  
    // get the (len-n+1)th node from 
    // the beginning
    for (i = 1; i < len - n + 1; i++)
        temp = temp->next;
  
    cout << temp->data;
  
    return;
}
  
void push(struct Node** head_ref, 
          int new_data)
{
    // Allocate node
    struct Node* new_node = new Node();
  
    // Put in the data 
    new_node->data = new_data;
  
    // Link the old list off the 
    // new node 
    new_node->next = (*head_ref);
  
    // Move the head to point to 
    // the new node 
    (*head_ref) = new_node;
}
  
// Driver Code
int main()
{
    // Start with the empty list 
    struct Node* head = NULL;
  
    // Create linked 35->15->4->20
    push(&head, 20);
    push(&head, 4);
    push(&head, 15);
    push(&head, 35);
  
    printNthFromLast(head, 4);
    return 0;
}


C++
void printNthFromLast(struct Node* head, int n)
{
    int i = 0;
    if (head == NULL)
        return;
    printNthFromLast(head->next, n);
    if (++i == n)
        cout<data;
}


C++
// Simple C++ program to 
// find n'th node from end
#include
using namespace std;
  
/* Link list node */
struct Node
{
  int data;
  struct Node* next;
};
  
/* Function to get the nth node 
   from the last of a linked list*/
void printNthFromLast(struct Node *head, int n)
{
  struct Node *main_ptr = head;
  struct Node *ref_ptr = head;
  
  int count = 0;
  if(head != NULL)
  {
     while( count < n )
     {
        if(ref_ptr == NULL)
        {
           printf("%d is greater than the no. of "
                    "nodes in list", n);
           return;
        }
        ref_ptr = ref_ptr->next;
        count++;
     } /* End of while*/
      
     if(ref_ptr == NULL)
     {
        head = head->next;
        if(head != NULL)
            printf("Node no. %d from last is %d ", n, main_ptr->data);
     }
     else
     {
       while(ref_ptr != NULL)
       {
          main_ptr = main_ptr->next;
          ref_ptr  = ref_ptr->next;
       }
       printf("Node no. %d from last is %d ", n, main_ptr->data);
     }
  }
}
  
// Function to push
void push(struct Node** head_ref, int new_data)
{
  /* allocate node */
  struct Node* new_node = new Node(); 
  
  /* put in the data  */
  new_node->data  = new_data;
  
  /* link the old list off the new node */
  new_node->next = (*head_ref);    
  
  /* move the head to point to the new node */
  (*head_ref)    = new_node;
}
  
/* Driver program to test above function*/
int main()
{
  /* Start with the empty list */
  struct Node* head = NULL;
  push(&head, 20);
  push(&head, 4);
  push(&head, 15);
  push(&head, 35);
  
  printNthFromLast(head, 4);
}


输出:

35

C++

void printNthFromLast(struct Node* head, int n)
{
    int i = 0;
    if (head == NULL)
        return;
    printNthFromLast(head->next, n);
    if (++i == n)
        cout<data;
}

时间复杂度: O(n),其中 n 是链表的长度。
方法2(使用两个指针)
维护两个指针——引用指针和主指针。初始化指向头的引用和主指针。首先,将引用指针从 head 移动到 n 个节点。现在将两个指针一一移动,直到引用指针到达末尾。现在主指针将指向从末尾开始的第 n 个节点。返回主指针。
下图是上述方法的试运行:

下面是上述方法的实现:

C++

// Simple C++ program to 
// find n'th node from end
#include
using namespace std;
  
/* Link list node */
struct Node
{
  int data;
  struct Node* next;
};
  
/* Function to get the nth node 
   from the last of a linked list*/
void printNthFromLast(struct Node *head, int n)
{
  struct Node *main_ptr = head;
  struct Node *ref_ptr = head;
  
  int count = 0;
  if(head != NULL)
  {
     while( count < n )
     {
        if(ref_ptr == NULL)
        {
           printf("%d is greater than the no. of "
                    "nodes in list", n);
           return;
        }
        ref_ptr = ref_ptr->next;
        count++;
     } /* End of while*/
      
     if(ref_ptr == NULL)
     {
        head = head->next;
        if(head != NULL)
            printf("Node no. %d from last is %d ", n, main_ptr->data);
     }
     else
     {
       while(ref_ptr != NULL)
       {
          main_ptr = main_ptr->next;
          ref_ptr  = ref_ptr->next;
       }
       printf("Node no. %d from last is %d ", n, main_ptr->data);
     }
  }
}
  
// Function to push
void push(struct Node** head_ref, int new_data)
{
  /* allocate node */
  struct Node* new_node = new Node(); 
  
  /* put in the data  */
  new_node->data  = new_data;
  
  /* link the old list off the new node */
  new_node->next = (*head_ref);    
  
  /* move the head to point to the new node */
  (*head_ref)    = new_node;
}
  
/* Driver program to test above function*/
int main()
{
  /* Start with the empty list */
  struct Node* head = NULL;
  push(&head, 20);
  push(&head, 4);
  push(&head, 15);
  push(&head, 35);
  
  printNthFromLast(head, 4);
}
输出
Node no. 4 from last is 35 

时间复杂度: O(n),其中 n 是链表的长度。
有关详细信息,请参阅有关链接列表末尾第 n 个节点的程序的完整文章!