📌  相关文章
📜  如何使用递归在给定位置的单链表中插入节点

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

如何使用递归在给定位置的单链表中插入节点

给定一个由列表位置节点组成的单链表,任务是使用递归将该元素插入给定链表中的给定位置。

例子:

解决方案:

会有3种情况

  1. 在链表的第一个位置插入一个节点
    • 方法:按照下面提到的步骤:
      1. 创建一个临时节点。
      2. 现在在 head 之前插入临时节点,并将 head 指针更改为指向临时节点。
  2. 在链表的第一个和最后一个节点之间插入一个节点
    • 方法:按照下面提到的步骤:
      1. 调用递归函数到达所需位置。
      2. 如果位置大于列表的长度,则无法插入。
      3. 如果没有,则在所需位置插入新节点。
  3. 在链表末尾插入一个节点
    • 方法:按照下面提到的步骤:
      1. 递归移动到链表的末尾。
      2. 在列表末尾插入新节点。

递归关系: T(n) = T(n-1) + c

以下是上述方法的实现

C++
// C++ code to insert a node
// at a given position
// in singly linked list
#include 
#define null nullptr
 
using namespace std;
 
// Structure of the node of linked list
struct node {
    int item;
    node* nxt;
    node(int item, node* t)
    {
        this->item = item;
        (*this).nxt = t;
    }
};
 
// Function to insert a node
// at the first position
node* insertAtFirst(node*& listHead, node* x)
{
    node* temp = listHead;
    x->nxt = temp;
    listHead = temp = x;
    return temp;
}
 
// Function to insert a node
// at the last position
node* insertAtEnd(node* listHead, node* x)
{
    if (listHead->nxt == null) {
        listHead->nxt = x;
        return listHead;
    }
    listHead->nxt
      = insertAtEnd(listHead->nxt, x);
    return listHead;
}
 
// Function to insert a node
// in between first and last
node* insertInBetween(node* temp,
                      node* x, int pos)
{
    if (pos == 2) {
        if (temp != null) {
            x->nxt = temp->nxt;
            temp->nxt = x;
        }
        return temp;
    }
    if (temp == null) {
        return temp;
    }
    temp->nxt
      = insertInBetween(temp->nxt, x, --pos);
}
 
// Printing through recursion
void print(node* head)
{
    if (head == null) {
        return;
    }
    cout << head->item << " ";
    print(head->nxt);
}
 
// Driver code
int main()
{
    node* head = new node(1, 0);
 
    // Creating a linked list of length 15
    for (int i = 2; i < 16; i++)
        insertAtEnd(head, new node(i, 0));
 
    // Insert node with value 100
    // at 4th position
    insertInBetween(head,
                    new node(100, 0), 4);
 
    // Insert node with value 101
    // at 200th position
    insertInBetween(head,
                    new node(101, 0), 200);
 
    // Insert node with value 100
    // at 1st position
    insertAtFirst(head, new node(100, 0));
 
    // Insert node with value 100
    // at the end position
    insertAtEnd(head, new node(100, 0));
 
    // Printing the new linked list
    print(head);
    return 0;
}


Java
// Java code to insert a node at a given
//position in singly linked list
class GFG{
 
// Structure of the node of linked list
static class node
{
    int item;
    node nxt;
     
    node(int item, node t)
    {
        this.item = item;
        this.nxt = t;
    }
};
 
// Function to insert a node
// at the first position
static node insertAtFirst(node listHead, node x)
{
    x.nxt = listHead;
    listHead = null;
    listHead = x;
    return listHead;
}
 
// Function to insert a node
// at the last position
static node insertAtEnd(node listHead, node x)
{
    if (listHead.nxt == null)
    {
        listHead.nxt = x;
        return listHead;
    }
    listHead.nxt = insertAtEnd(listHead.nxt, x);
    return listHead;
}
 
// Function to insert a node
// in between first and last
static node insertInBetween(node temp, node x,
                            int pos)
{
    if (pos == 2)
    {
        if (temp != null)
        {
            x.nxt = temp.nxt;
            temp.nxt = x;
        }
        return temp;
    }
    if (temp == null)
    {
        return temp;
    }
    temp.nxt = insertInBetween(temp.nxt, x, --pos);
    return temp;
}
 
// Printing through recursion
static void print(node head)
{
    if (head == null)
    {
        return;
    }
    System.out.print(head.item + " ");
    print(head.nxt);
}
 
// Driver code
public static void main(String[] args)
{
    node head = new node(1, null);
 
    // Creating a linked list of length 15
    for(int i = 2; i < 16; i++)
        insertAtEnd(head, new node(i, null));
 
    // Insert node with value 100
    // at 4th position
    head = insertInBetween(head,
                    new node(100, null), 4);
 
    // Insert node with value 101
    // at 200th position
    head = insertInBetween(head,
                    new node(101, null), 200);
 
    // Insert node with value 100
    // at 1st position
    head = insertAtFirst(head, new node(100, null));
 
    // Insert node with value 100
    // at the end position
    head = insertAtEnd(head, new node(100, null));
 
    // Printing the new linked list
    print(head);
}
}
 
// This code is contributed by shikhasingrajput


C#
// C# code to insert a node at a given
//position in singly linked list
using System;
 
public class GFG{
 
// Structure of the node of linked list
class node
{
    public int item;
    public node nxt;
     
    public node(int item, node t)
    {
        this.item = item;
        this.nxt = t;
    }
};
 
// Function to insert a node
// at the first position
static node insertAtFirst(node listHead, node x)
{
    x.nxt = listHead;
    listHead = null;
    listHead = x;
    return listHead;
}
 
// Function to insert a node
// at the last position
static node insertAtEnd(node listHead, node x)
{
    if (listHead.nxt == null)
    {
        listHead.nxt = x;
        return listHead;
    }
    listHead.nxt = insertAtEnd(listHead.nxt, x);
    return listHead;
}
 
// Function to insert a node
// in between first and last
static node insertInBetween(node temp, node x,
                            int pos)
{
    if (pos == 2)
    {
        if (temp != null)
        {
            x.nxt = temp.nxt;
            temp.nxt = x;
        }
        return temp;
    }
    if (temp == null)
    {
        return temp;
    }
    temp.nxt = insertInBetween(temp.nxt, x, --pos);
    return temp;
}
 
// Printing through recursion
static void print(node head)
{
    if (head == null)
    {
        return;
    }
    Console.Write(head.item + " ");
    print(head.nxt);
}
 
// Driver code
public static void Main(String[] args)
{
    node head = new node(1, null);
 
    // Creating a linked list of length 15
    for(int i = 2; i < 16; i++)
        insertAtEnd(head, new node(i, null));
 
    // Insert node with value 100
    // at 4th position
    head = insertInBetween(head,
                    new node(100, null), 4);
 
    // Insert node with value 101
    // at 200th position
    head = insertInBetween(head,
                    new node(101, null), 200);
 
    // Insert node with value 100
    // at 1st position
    head = insertAtFirst(head, new node(100, null));
 
    // Insert node with value 100
    // at the end position
    head = insertAtEnd(head, new node(100, null));
 
    // Printing the new linked list
    print(head);
}
}
 
// This code is contributed by shikhasingrajput


Javascript


C++
#include 
using namespace std;
 
class Node {
public:
    int data;
    Node* next;
 
    Node(int data)
    {
        this->data = data;
        next = NULL;
    }
};
 
Node* insertatk(Node* head, int k, int data)
{
    if (head == NULL)
        return new Node(data);
 
      if (k == 1) {
        Node* newnode = new Node(data);
        newnode->next = head;
        head = newnode;
          return head;
    }
    else
        head->next=insertatk(head->next, k-1, data);//we do k-1 so as to reach the required place
   
      return head;
}
 
void print(Node* head)
{
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << "\n";
}
 
int main()
{
      //inserting nodes and connecting them at the same time
    Node* head = new Node(1);
    Node* n1 = new Node(2);
    head->next = n1;
    Node* n2 = new Node(3);
    n1->next = n2;
    Node* n3 = new Node(4);
    n2->next = n3;
    Node* n4 = new Node(5);
    n3->next = n4;
    Node* n5 = new Node(6);
    n4->next = n5;
      Node* n6 = new Node(7);
    n5->next = n6;
    n6->next = NULL;
 
    int k = 4;
    int data = 100;
   
    print(insertatk(head, k, data));
    return 0;
}


输出
100 1 2 3 100 4 5 6 7 8 9 10 11 12 13 14 15 100 

时间复杂度: O(N) 其中 N 是链表的大小
辅助空间: O(N) 其中 N 是链表的大小

上述 C++ 代码的一种更简单的方法:

C++

#include 
using namespace std;
 
class Node {
public:
    int data;
    Node* next;
 
    Node(int data)
    {
        this->data = data;
        next = NULL;
    }
};
 
Node* insertatk(Node* head, int k, int data)
{
    if (head == NULL)
        return new Node(data);
 
      if (k == 1) {
        Node* newnode = new Node(data);
        newnode->next = head;
        head = newnode;
          return head;
    }
    else
        head->next=insertatk(head->next, k-1, data);//we do k-1 so as to reach the required place
   
      return head;
}
 
void print(Node* head)
{
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << "\n";
}
 
int main()
{
      //inserting nodes and connecting them at the same time
    Node* head = new Node(1);
    Node* n1 = new Node(2);
    head->next = n1;
    Node* n2 = new Node(3);
    n1->next = n2;
    Node* n3 = new Node(4);
    n2->next = n3;
    Node* n4 = new Node(5);
    n3->next = n4;
    Node* n5 = new Node(6);
    n4->next = n5;
      Node* n6 = new Node(7);
    n5->next = n6;
    n6->next = NULL;
 
    int k = 4;
    int data = 100;
   
    print(insertatk(head, k, data));
    return 0;
}
输出
1 2 3 100 4 5 6 7