原文:

给定一个链表,请交替打印此链表节点。

示例

input : 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
output : 1 -> 3 -> 5 -> 7 -> 9 
input : 10 -> 9
output : 10

递归方法

  1. 初始化静态变量(例如标记)

  2. 如果标记为奇数,则打印节点

  3. 将头和标记加 1,然后递归返回下一个节点。

c

// cpp code to print alternate nodes 
// of a linked list using recursion 
#include  
using namespace std; 
// a linked list node 
struct node { 
    int data; 
    struct node* next; 
}; 
// inserting node at the beginning 
void push(struct node** head_ref, int new_data) 
{  
    struct node* new_node =  
       (struct node*)malloc(sizeof(struct node)); 
    new_node->data = new_data; 
    new_node->next = (*head_ref); 
    (*head_ref) = new_node; 
} 
// function to print alternate nodes of linked list. 
// the boolean flag isodd is used to find if the current 
// node is even or odd. 
void printalternate(struct node* node, bool isodd=true) 
{ 
    if (node == null) 
       return; 
    if (isodd == true) 
        cout << node->data << " ";  
    printalternate(node->next, !isodd); 
} 
// driver code 
int main() 
{ 
    // start with the empty list 
    struct node* head = null; 
    // construct below list 
    // 1->2->3->4->5->6->7->8->9->10 
    push(&head, 10); 
    push(&head, 9); 
    push(&head, 8); 
    push(&head, 7); 
    push(&head, 6); 
    push(&head, 5); 
    push(&head, 4); 
    push(&head, 3); 
    push(&head, 2); 
    push(&head, 1); 
    printalternate(head); 
    return 0; 
} 

java

// java code to print alternate nodes  
// of a linked list using recursion  
class gfg  
{ 
// a linked list node  
static class node  
{  
    int data;  
    node next;  
};  
// inserting node at the beginning  
static node push( node head_ref, int new_data)  
{  
    node new_node = new node();  
    new_node.data = new_data;  
    new_node.next = (head_ref);  
    (head_ref) = new_node;  
    return head_ref; 
}  
// function to print alternate nodes of linked list.  
// the boolean flag isodd is used to find if the current  
// node is even or odd.  
static void printalternate( node node, boolean isodd)  
{  
    if (node == null)  
    return;  
    if (isodd == true)  
        system.out.print( node.data   " ");  
    printalternate(node.next, !isodd);  
}  
// driver code  
public static void main(string args[]) 
{  
    // start with the empty list  
    node head = null;  
    // con below list  
    // 1.2.3.4.5.6.7.8.9.10  
    head = push(head, 10);  
    head = push(head, 9);  
    head = push(head, 8);  
    head = push(head, 7);  
    head = push(head, 6);  
    head = push(head, 5);  
    head = push(head, 4);  
    head = push(head, 3);  
    head = push(head, 2);  
    head = push(head, 1);  
    printalternate(head,true);  
} 
} 
// this code is contributed by arnab kundu 

python3

# python3 code to print alternate nodes  
# of a linked list using recursion  
# a linked list node 
class node:  
    def __init__(self, data):  
        self.data = data  
        self.next = none
# inserting node at the beginning  
def push( head_ref, new_data): 
    new_node = node(new_data); 
    new_node.data = new_data;  
    new_node.next = head_ref;  
    head_ref = new_node;  
    return head_ref;  
# function to print alternate nodes of  
# linked list. the boolean flag isodd  
# is used to find if the current node 
# is even or odd.  
def printalternate( node, isodd): 
    if (node == none): 
        return;  
    if (isodd == true): 
        print( node.data, end = " ");  
    if (isodd == true): 
        isodd = false; 
    else: 
        isodd = true; 
    printalternate(node.next, isodd);  
# driver code  
# start with the empty list  
head = none;  
# con below list  
# 1->2->3->4->5->6->7->8->9->10  
head = push(head, 10);  
head = push(head, 9);  
head = push(head, 8);  
head = push(head, 7);  
head = push(head, 6);  
head = push(head, 5);  
head = push(head, 4);  
head = push(head, 3);  
head = push(head, 2);  
head = push(head, 1);  
printalternate(head, true);  
# this code is contributed by 29ajaykumar 

c

// c# code to print alternate nodes  
// of a linked list using recursion  
using system; 
class gfg  
{ 
// a linked list node  
public class node  
{  
    public int data;  
    public node next;  
};  
// inserting node at the beginning  
static node push( node head_ref, int new_data)  
{  
    node new_node = new node();  
    new_node.data = new_data;  
    new_node.next = (head_ref);  
    (head_ref) = new_node;  
    return head_ref; 
}  
// function to print alternate nodes of linked list.  
// the boolean flag isodd is used to find if the current  
// node is even or odd.  
static void printalternate( node node, bool isodd)  
{  
    if (node == null)  
    return;  
    if (isodd == true)  
        console.write( node.data   " ");  
    printalternate(node.next, !isodd);  
}  
// driver code  
public static void main(string []args) 
{  
    // start with the empty list  
    node head = null;  
    // con below list  
    // 1.2.3.4.5.6.7.8.9.10  
    head = push(head, 10);  
    head = push(head, 9);  
    head = push(head, 8);  
    head = push(head, 7);  
    head = push(head, 6);  
    head = push(head, 5);  
    head = push(head, 4);  
    head = push(head, 3);  
    head = push(head, 2);  
    head = push(head, 1);  
    printalternate(head,true);  
} 
} 
// this code has been contributed by 29ajaykumar 

输出

1 3 5 7 9


如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。