展开折叠链表的程序
链表L 0 -> L 1 -> L 2 -> ..... -> L N可以折叠为L 0 -> L N -> L 1 -> L N – 1 -> L 2 -> ...。 .
给定一个折叠链表,任务是展开并打印原始链表
例子:
Input: 1 -> 6 -> 2 -> 5 -> 3 -> 4
Output: 1 2 3 4 5 6
Input: 1 -> 5 -> 2 -> 4 -> 3
Output: 1 2 3 4 5
方法:进行递归调用并将下一个节点存储在临时指针中,第一个节点将作为头节点,存储在临时指针中的节点将作为列表的尾部。在达到基本条件后返回时,将头部和尾部分别链接到前一个头部和尾部。
基本条件:如果节点数是偶数,则倒数第二个节点是头,最后一个节点是尾,如果节点数是奇数,则最后一个节点将充当头和尾。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Node Class
struct Node
{
int data;
Node *next;
};
// Head of the list
Node *head;
// Tail of the list
Node *tail;
// Function to print the list
void display()
{
if (head == NULL)
return;
Node* temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
// Function to add node in the list
void push(int data)
{
// Create new node
Node* nn = new Node();
nn->data = data;
nn->next = NULL;
// Linking at first position
if (head == NULL)
{
head = nn;
}
else
{
Node* temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
// Linking at last in list
temp->next = nn;
}
}
// Function to unfold the given link list
void unfold(Node* node)
{
if (node == NULL)
return;
// This condition will reach if
// the number of nodes is odd
// head and tail is same i->e-> last node
if (node->next == NULL)
{
head = tail = node;
return;
}
// This base condition will reach if
// the number of nodes is even
// mark head to the second last node
// and tail to the last node
else if (node->next->next == NULL)
{
head = node;
tail = node->next;
return;
}
// Storing next node in temp pointer
// before making the recursive call
Node* temp = node->next;
// Recursive call
unfold(node->next->next);
// Connecting first node to head
// and mark it as a new head
node->next = head;
head = node;
// Connecting tail to second node (temp)
// and mark it as a new tail
tail->next = temp;
tail = temp;
tail->next = NULL;
}
// Driver code
int main()
{
// Adding nodes to the list
push(1);
push(5);
push(2);
push(4);
push(3);
// Displaying the original nodes
display();
// Calling unfold function
unfold(head);
// Displaying the list
// after modification
display();
}
// This code is contributed by pratham76
Java
// Java implementation of the approach
public class GFG {
// Node Class
private class Node {
int data;
Node next;
}
// Head of the list
private Node head;
// Tail of the list
private Node tail;
// Function to print the list
public void display()
{
if (head == null)
return;
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
// Function to add node in the list
public void push(int data)
{
// Create new node
Node nn = new Node();
nn.data = data;
nn.next = null;
// Linking at first position
if (head == null) {
head = nn;
}
else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
// Linking at last in list
temp.next = nn;
}
}
// Function to unfold the given link list
private void unfold(Node node)
{
if (node == null)
return;
// This condition will reach if
// the number of nodes is odd
// head and tail is same i.e. last node
if (node.next == null) {
head = tail = node;
return;
}
// This base condition will reach if
// the number of nodes is even
// mark head to the second last node
// and tail to the last node
else if (node.next.next == null) {
head = node;
tail = node.next;
return;
}
// Storing next node in temp pointer
// before making the recursive call
Node temp = node.next;
// Recursive call
unfold(node.next.next);
// Connecting first node to head
// and mark it as a new head
node.next = head;
head = node;
// Connecting tail to second node (temp)
// and mark it as a new tail
tail.next = temp;
tail = temp;
tail.next = null;
}
// Driver code
public static void main(String[] args)
{
GFG l = new GFG();
// Adding nodes to the list
l.push(1);
l.push(5);
l.push(2);
l.push(4);
l.push(3);
// Displaying the original nodes
l.display();
// Calling unfold function
l.unfold(l.head);
// Displaying the list
// after modification
l.display();
}
}
Python3
# Python3 implementation of the approach
# Node Class
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Head of the list
head = None
# Tail of the list
tail = None
# Function to print the list
def display():
if (head == None):
return
temp = head
while (temp != None):
print(temp.data, end = " ")
temp = temp.next
print()
# Function to add node in the list
def push(data):
global head, tail
# Create new node
nn = Node(data)
# Linking at first positio
if (head == None):
head = nn
else:
temp = head
while (temp.next != None):
temp = temp.next
# Linking at last in list
temp.next = nn
# Function to unfold the given link list
def unfold(node):
global head, tail
if (node == None):
return
# This condition will reach if
# the number of nodes is odd
# head and tail is same i.e. last node
if (node.next == None):
head = tail = node
return
# This base condition will reach if
# the number of nodes is even
# mark head to the second last node
# and tail to the last node
elif (node.next.next == None):
head = node
tail = node.next
return
# Storing next node in temp pointer
# before making the recursive call
temp = node.next
# Recursive call
unfold(node.next.next)
# Connecting first node to head
# and mark it as a new head
node.next = head
head = node
# Connecting tail to second node (temp)
# and mark it as a new tail
tail.next = temp
tail = temp
tail.next = None
# Driver code
if __name__=='__main__':
# Adding nodes to the list
push(1)
push(5)
push(2)
push(4)
push(3)
# Displaying the original nodes
display()
# Calling unfold function
unfold(head)
# Displaying the list
# after modification
display()
# This code is contributed by rutvik_56
C#
// C# implementation of the approach
using System;
public class GFG {
// Node Class
private class Node {
public int data;
public Node next;
}
// Head of the list
private Node head;
// Tail of the list
private Node tail;
// Function to print the list
public void display()
{
if (head == null)
return;
Node temp = head;
while (temp != null) {
Console.Write(temp.data + " ");
temp = temp.next;
}
Console.WriteLine();
}
// Function to add node in the list
public void push(int data)
{
// Create new node
Node nn = new Node();
nn.data = data;
nn.next = null;
// Linking at first position
if (head == null) {
head = nn;
}
else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
// Linking at last in list
temp.next = nn;
}
}
// Function to unfold the given link list
private void unfold(Node node)
{
if (node == null)
return;
// This condition will reach if
// the number of nodes is odd
// head and tail is same i.e. last node
if (node.next == null) {
head = tail = node;
return;
}
// This base condition will reach if
// the number of nodes is even
// mark head to the second last node
// and tail to the last node
else if (node.next.next == null) {
head = node;
tail = node.next;
return;
}
// Storing next node in temp pointer
// before making the recursive call
Node temp = node.next;
// Recursive call
unfold(node.next.next);
// Connecting first node to head
// and mark it as a new head
node.next = head;
head = node;
// Connecting tail to second node (temp)
// and mark it as a new tail
tail.next = temp;
tail = temp;
tail.next = null;
}
// Driver code
public static void Main()
{
GFG l = new GFG();
// Adding nodes to the list
l.push(1);
l.push(5);
l.push(2);
l.push(4);
l.push(3);
// Displaying the original nodes
l.display();
// Calling unfold function
l.unfold(l.head);
// Displaying the list
// after modification
l.display();
}
}
/* This code contributed by PrinciRaj1992 */
Javascript
输出:
1 5 2 4 3
1 2 3 4 5
时间复杂度:O(N),其中 N 是链表中的节点总数。
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。