📜  递归插入和遍历链表

📅  最后修改于: 2021-04-29 07:53:37             🧑  作者: Mango

我们讨论了链表插入的不同方法。如何递归地创建一个链表?

递归插入到末尾:
要使用递归创建链接列表,请按照下列步骤操作。下面的步骤在链表的末尾递归插入一个新节点。

C++
// Function to insert a new node at the
// end of linked list using recursion.
Node* insertEnd(Node* head, int data)
{
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == NULL)
         return newNode(data);
 
    // If we have not reached end, keep traversing
    // recursively.
    else
        head->next = insertEnd(head->next, data);
    return head;
}


Java
// Function to insert a new node at the
// end of linked list using recursion.
static Node insertEnd(Node head, int data)
{
   
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == null)
         return newNode(data);
  
    // If we have not reached end, keep traversing
    // recursively.
    else
        head.next = insertEnd(head.next, data);
   
    return head;
}
 
// This code is contributed by rutvik_56


Python3
# Function to insert a new node at the
# end of linked list using recursion.
def insertEnd(head, data):
   
    # If linked list is empty, create a
    # new node (Assuming newNode() allocates
    # a new node with given data)
    if (head == None):
        return newNode(data)
  
    # If we have not reached end,
    # keep traversing recursively.
    else:
        head.next = insertEnd(head.next, data)
         
    return head
 
# This code is contributed by divyeshrabadiya07


C#
// Function to insert a new node at the 
// end of linked list using recursion. 
static Node insertEnd(Node head, int data) 
{ 
   
    // If linked list is empty, create a 
    // new node (Assuming newNode() allocates 
    // a new node with given data) 
    if (head == null) 
        return newNode(data); 
   
    // If we have not reached end, keep traversing 
    // recursively. 
    else
        head.next = insertEnd(head.next, data); 
   
    return head; 
}
 
// This code is contributed by divyesh072019


C++
void traverse(Node* head)
{
    if (head == NULL)
       return;
     
    // If head is not NULL, print current node
    // and recur for remaining list  
    cout << head->data << " ";
 
    traverse(head->next);
}


Python3
def traverse(head):
    if (head == None):
        return
      
    # If head is not None, print current node
    # and recur for remaining list
    print(head.data, end = " ");
    traverse(head.next)
     
    # This code is contributed by Pratham76


C++
// Recursive C++ program to recursively insert
// a node and recursively print the list.
#include 
using namespace std;
struct Node {
    int data;
    Node* next;
};
 
// Allocates a new node with given data
Node *newNode(int data)
{
    Node *new_node = new Node;
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}
 
// Function to insert a new node at the
// end of linked list using recursion.
Node* insertEnd(Node* head, int data)
{
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == NULL)
         return newNode(data);
 
    // If we have not reached end, keep traversing
    // recursively.
    else
        head->next = insertEnd(head->next, data);
    return head;
}
 
void traverse(Node* head)
{
    if (head == NULL)
       return;
     
    // If head is not NULL, print current node
    // and recur for remaining list  
    cout << head->data << " ";
 
    traverse(head->next);
}
 
// Driver code
int main()
{
    Node* head = NULL;
    head = insertEnd(head, 6);
    head = insertEnd(head, 8);
    head = insertEnd(head, 10);
    head = insertEnd(head, 12);
    head = insertEnd(head, 14);
    traverse(head);
}


Java
// Recursive Java program to recursively insert
// a node and recursively print the list.
class GFG
{
     
static class Node
{
    int data;
    Node next;
};
 
// Allocates a new node with given data
static Node newNode(int data)
{
    Node new_node = new Node();
    new_node.data = data;
    new_node.next = null;
    return new_node;
}
 
// Function to insert a new node at the
// end of linked list using recursion.
static Node insertEnd(Node head, int data)
{
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == null)
        return newNode(data);
 
    // If we have not reached end, keep traversing
    // recursively.
    else
        head.next = insertEnd(head.next, data);
    return head;
}
 
static void traverse(Node head)
{
    if (head == null)
    return;
     
    // If head is not null, print current node
    // and recur for remaining list
    System.out.print( head.data + " ");
 
    traverse(head.next);
}
 
// Driver code
public static void main(String args[])
{
    Node head = null;
    head = insertEnd(head, 6);
    head = insertEnd(head, 8);
    head = insertEnd(head, 10);
    head = insertEnd(head, 12);
    head = insertEnd(head, 14);
    traverse(head);
}
}
 
// This code is contributed by andrew1234


Python3
# Recursive Python3 program to
# recursively insert a node and
# recursively print the list.
import math
 
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Allocates a new node with given data
def newNode(data):
    new_node = Node(data)
    new_node.data = data
    new_node.next = None
    return new_node
 
# Function to insert a new node at the
# end of linked list using recursion.
def insertEnd(head, data):
     
    # If linked list is empty, create a
    # new node (Assuming newNode() allocates
    # a new node with given data)
    if (head == None):
        return newNode(data)
 
    # If we have not reached end,
    # keep traversing recursively.
    else:
        head.next = insertEnd(head.next, data)
    return head
 
def traverse(head):
    if (head == None):
        return
     
    # If head is not None, print current node
    # and recur for remaining list
    print(head.data, end = " ");
    traverse(head.next)
 
# Driver code
if __name__=='__main__':
    head = None
    head = insertEnd(head, 6)
    head = insertEnd(head, 8)
    head = insertEnd(head, 10)
    head = insertEnd(head, 12)
    head = insertEnd(head, 14)
    traverse(head)
 
# This code is contributed by sapna singh


C#
// Recursive C# program to recursively insert
// a node and recursively print the list.
using System;
 
class GFG
{
     
public class Node
{
    public int data;
    public Node next;
};
 
// Allocates a new node with given data
static Node newNode(int data)
{
    Node new_node = new Node();
    new_node.data = data;
    new_node.next = null;
    return new_node;
}
 
// Function to insert a new node at the
// end of linked list using recursion.
static Node insertEnd(Node head, int data)
{
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == null)
        return newNode(data);
 
    // If we have not reached end, keep traversing
    // recursively.
    else
        head.next = insertEnd(head.next, data);
    return head;
}
 
static void traverse(Node head)
{
    if (head == null)
    return;
     
    // If head is not null, print current node
    // and recur for remaining list
    Console.Write(head.data + " ");
 
    traverse(head.next);
}
 
// Driver code
public static void Main(String []args)
{
    Node head = null;
    head = insertEnd(head, 6);
    head = insertEnd(head, 8);
    head = insertEnd(head, 10);
    head = insertEnd(head, 12);
    head = insertEnd(head, 14);
    traverse(head);
}
}
 
// This code is contributed by 29AjayKumar


递归遍历列表:
这个想法很简单,我们打印当前节点,然后递归剩余的列表。

C++

void traverse(Node* head)
{
    if (head == NULL)
       return;
     
    // If head is not NULL, print current node
    // and recur for remaining list  
    cout << head->data << " ";
 
    traverse(head->next);
}

Python3

def traverse(head):
    if (head == None):
        return
      
    # If head is not None, print current node
    # and recur for remaining list
    print(head.data, end = " ");
    traverse(head.next)
     
    # This code is contributed by Pratham76

完整程序:
以下是演示插入和遍历链表工作的完整程序。

C++

// Recursive C++ program to recursively insert
// a node and recursively print the list.
#include 
using namespace std;
struct Node {
    int data;
    Node* next;
};
 
// Allocates a new node with given data
Node *newNode(int data)
{
    Node *new_node = new Node;
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}
 
// Function to insert a new node at the
// end of linked list using recursion.
Node* insertEnd(Node* head, int data)
{
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == NULL)
         return newNode(data);
 
    // If we have not reached end, keep traversing
    // recursively.
    else
        head->next = insertEnd(head->next, data);
    return head;
}
 
void traverse(Node* head)
{
    if (head == NULL)
       return;
     
    // If head is not NULL, print current node
    // and recur for remaining list  
    cout << head->data << " ";
 
    traverse(head->next);
}
 
// Driver code
int main()
{
    Node* head = NULL;
    head = insertEnd(head, 6);
    head = insertEnd(head, 8);
    head = insertEnd(head, 10);
    head = insertEnd(head, 12);
    head = insertEnd(head, 14);
    traverse(head);
}

Java

// Recursive Java program to recursively insert
// a node and recursively print the list.
class GFG
{
     
static class Node
{
    int data;
    Node next;
};
 
// Allocates a new node with given data
static Node newNode(int data)
{
    Node new_node = new Node();
    new_node.data = data;
    new_node.next = null;
    return new_node;
}
 
// Function to insert a new node at the
// end of linked list using recursion.
static Node insertEnd(Node head, int data)
{
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == null)
        return newNode(data);
 
    // If we have not reached end, keep traversing
    // recursively.
    else
        head.next = insertEnd(head.next, data);
    return head;
}
 
static void traverse(Node head)
{
    if (head == null)
    return;
     
    // If head is not null, print current node
    // and recur for remaining list
    System.out.print( head.data + " ");
 
    traverse(head.next);
}
 
// Driver code
public static void main(String args[])
{
    Node head = null;
    head = insertEnd(head, 6);
    head = insertEnd(head, 8);
    head = insertEnd(head, 10);
    head = insertEnd(head, 12);
    head = insertEnd(head, 14);
    traverse(head);
}
}
 
// This code is contributed by andrew1234

Python3

# Recursive Python3 program to
# recursively insert a node and
# recursively print the list.
import math
 
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Allocates a new node with given data
def newNode(data):
    new_node = Node(data)
    new_node.data = data
    new_node.next = None
    return new_node
 
# Function to insert a new node at the
# end of linked list using recursion.
def insertEnd(head, data):
     
    # If linked list is empty, create a
    # new node (Assuming newNode() allocates
    # a new node with given data)
    if (head == None):
        return newNode(data)
 
    # If we have not reached end,
    # keep traversing recursively.
    else:
        head.next = insertEnd(head.next, data)
    return head
 
def traverse(head):
    if (head == None):
        return
     
    # If head is not None, print current node
    # and recur for remaining list
    print(head.data, end = " ");
    traverse(head.next)
 
# Driver code
if __name__=='__main__':
    head = None
    head = insertEnd(head, 6)
    head = insertEnd(head, 8)
    head = insertEnd(head, 10)
    head = insertEnd(head, 12)
    head = insertEnd(head, 14)
    traverse(head)
 
# This code is contributed by sapna singh

C#

// Recursive C# program to recursively insert
// a node and recursively print the list.
using System;
 
class GFG
{
     
public class Node
{
    public int data;
    public Node next;
};
 
// Allocates a new node with given data
static Node newNode(int data)
{
    Node new_node = new Node();
    new_node.data = data;
    new_node.next = null;
    return new_node;
}
 
// Function to insert a new node at the
// end of linked list using recursion.
static Node insertEnd(Node head, int data)
{
    // If linked list is empty, create a
    // new node (Assuming newNode() allocates
    // a new node with given data)
    if (head == null)
        return newNode(data);
 
    // If we have not reached end, keep traversing
    // recursively.
    else
        head.next = insertEnd(head.next, data);
    return head;
}
 
static void traverse(Node head)
{
    if (head == null)
    return;
     
    // If head is not null, print current node
    // and recur for remaining list
    Console.Write(head.data + " ");
 
    traverse(head.next);
}
 
// Driver code
public static void Main(String []args)
{
    Node head = null;
    head = insertEnd(head, 6);
    head = insertEnd(head, 8);
    head = insertEnd(head, 10);
    head = insertEnd(head, 12);
    head = insertEnd(head, 14);
    traverse(head);
}
}
 
// This code is contributed by 29AjayKumar

输出:

6 8 10 12 14