📌  相关文章
📜  将最后M个节点附加到给定链表的开头

📅  最后修改于: 2021-06-28 19:43:34             🧑  作者: Mango

给定一个链表和一个整数M ,任务是将链表的最后M个节点附加到最前面。
例子:

方法:找到列表中最后M个节点中的第一个节点,该节点将是新的头节点,因此使上一个节点的下一个指针为NULL,并将原始列表的最后一个节点指向原始列表的头。最后,打印更新的列表。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Class for a node of
// the linked list
struct Node
{
 
    // Data and the pointer
    // to the next node
    int data;
    Node *next;
    Node(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};
 
// Function to print the linked list
void printList(Node* node)
{
  while (node != NULL)
  {
    cout << (node->data) << " -> ";
    node = node->next;
  }
  cout << "NULL";
}
 
// Recursive function to return the
// count of nodes in the linked list
int cntNodes(Node* node)
{
  if (node == NULL)
    return 0;
 
  return (1 + cntNodes(node->next));
}
 
// Function to update and print
// the updated list nodes
void updateList(Node* head, int m)
{
 
  // Total nodes in the list
  int cnt = cntNodes(head);
 
  if (cnt != m && m < cnt)
  {
 
    // Count of nodes to be skipped
    // from the beginning
    int skip = cnt - m;
    Node *prev = NULL;
    Node *curr = head;
 
    // Skip the nodes
    while (skip > 0)
    {
      prev = curr;
      curr = curr->next;
      skip--;
    }
 
    // Change the pointers
    prev->next = NULL;
    Node *tempHead = head;
    head = curr;
 
    // Find the last node
    while (curr->next != NULL)
      curr = curr->next;
 
    // Connect it to the head
    // of the sub list
    curr->next = tempHead;
  }
 
  // Print the updated list
  printList(head);
}
 
// Driver code
int main()
{
 
  // Create the list
  Node* head = new Node(4);
  head->next = new Node(5);
  head->next->next = new Node(6);
  head->next->next->next = new Node(1);
  head->next->next->next->next = new Node(2);
  head->next->next->next->next->next = new Node(3);
  int m = 3;
  updateList(head, m);
  return 0;
}
 
// This code is contributed by rutvik_56


Java
// Java implementation of the approach
class GFG {
 
    // Class for a node of
    // the linked list
    static class Node {
 
        // Data and the pointer
        // to the next node
        int data;
        Node next;
 
        Node(int data)
        {
            this.data = data;
            this.next = null;
        }
    }
 
    // Function to print the linked list
    static void printList(Node node)
    {
        while (node != null) {
            System.out.print(node.data + " -> ");
            node = node.next;
        }
        System.out.print("NULL");
    }
 
    // Recursive function to return the
    // count of nodes in the linked list
    static int cntNodes(Node node)
    {
        if (node == null)
            return 0;
 
        return (1 + cntNodes(node.next));
    }
 
    // Function to update and print
    // the updated list nodes
    static void updateList(Node head, int m)
    {
 
        // Total nodes in the list
        int cnt = cntNodes(head);
 
        if (cnt != m && m < cnt) {
 
            // Count of nodes to be skipped
            // from the beginning
            int skip = cnt - m;
            Node prev = null;
            Node curr = head;
 
            // Skip the nodes
            while (skip > 0) {
                prev = curr;
                curr = curr.next;
                skip--;
            }
 
            // Change the pointers
            prev.next = null;
            Node tempHead = head;
            head = curr;
 
            // Find the last node
            while (curr.next != null)
                curr = curr.next;
 
            // Connect it to the head
            // of the sub list
            curr.next = tempHead;
        }
 
        // Print the updated list
        printList(head);
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Create the list
        Node head = new Node(4);
        head.next = new Node(5);
        head.next.next = new Node(6);
        head.next.next.next = new Node(1);
        head.next.next.next.next = new Node(2);
        head.next.next.next.next.next = new Node(3);
 
        int m = 3;
 
        updateList(head, m);
    }
}


Python3
# Python3 implementation of the approach
 
# Class for a node of
# the linked list
class newNode:
     
    # Constructor to initialize the node object
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Function to print the linked list
def printList(node):
     
    while (node != None):
        print(node.data,"->", end=" ")
        node = node.next
    print("NULL")
 
# Recursive function to return the
# count of nodes in the linked list
def cntNodes(node):
    if (node == None):
        return 0
         
    return (1 + cntNodes(node.next))
     
# Function to update and print
# the updated list nodes
def updateList(head, m):
     
    # Total nodes in the list
    cnt = cntNodes(head)
     
    if (cnt != m and m < cnt):
         
        # Count of nodes to be skipped
        # from the beginning
        skip = cnt - m
        prev = None
         
        curr = head
         
        # Skip the nodes
        while (skip > 0):
            prev = curr
            curr = curr.next
            skip-=1
             
        # Change the pointers
        prev.next = None
        tempHead = head
        head = curr
         
        # Find the last node
        while (curr.next != None):
            curr = curr.next
             
        # Connect it to the head
        # of the sub list
        curr.next = tempHead
     
    # Print the updated list
    printList(head)
 
# Driver code
 
# Create the list
head = newNode(4)
head.next = newNode(5)
head.next.next = newNode(6)
head.next.next.next = newNode(1)
head.next.next.next.next = newNode(2)
head.next.next.next.next.next = newNode(3)
 
m = 3
 
updateList(head, m)
 
# This code is contributed by shubhamsingh10


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Class for a node of
    // the linked list
    class Node
    {
 
        // Data and the pointer
        // to the next node
        public int data;
        public Node next;
 
        public Node(int data)
        {
            this.data = data;
            this.next = null;
        }
    }
 
    // Function to print the linked list
    static void printList(Node node)
    {
        while (node != null)
        {
            Console.Write(node.data + " -> ");
            node = node.next;
        }
        Console.Write("NULL");
    }
 
    // Recursive function to return the
    // count of nodes in the linked list
    static int cntNodes(Node node)
    {
        if (node == null)
            return 0;
 
        return (1 + cntNodes(node.next));
    }
 
    // Function to update and print
    // the updated list nodes
    static void updateList(Node head, int m)
    {
 
        // Total nodes in the list
        int cnt = cntNodes(head);
 
        if (cnt != m && m < cnt)
        {
 
            // Count of nodes to be skipped
            // from the beginning
            int skip = cnt - m;
            Node prev = null;
            Node curr = head;
 
            // Skip the nodes
            while (skip > 0)
            {
                prev = curr;
                curr = curr.next;
                skip--;
            }
 
            // Change the pointers
            prev.next = null;
            Node tempHead = head;
            head = curr;
 
            // Find the last node
            while (curr.next != null)
                curr = curr.next;
 
            // Connect it to the head
            // of the sub list
            curr.next = tempHead;
        }
 
        // Print the updated list
        printList(head);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
 
        // Create the list
        Node head = new Node(4);
        head.next = new Node(5);
        head.next.next = new Node(6);
        head.next.next.next = new Node(1);
        head.next.next.next.next = new Node(2);
        head.next.next.next.next.next = new Node(3);
 
        int m = 3;
 
        updateList(head, m);
    }
}
 
// This code is contributed by PrinciRaj1992


输出:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL