原文:
给定一个链表,交替打印链表的节点。
例子:
input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42
output : 1 -> 3 -> 17 -> 29
alternate nodes : 1 -> 3 -> 17 -> 29
input : 10 -> 17 -> 33 -> 38 -> 73
output : 10 -> 33 -> 73
alternate nodes : 10 -> 33 -> 73
方法:
-
遍历整个链表。
-
设置计数为 0。
-
当计数为偶数时打印节点。
-
访问下一个节点。
c
// cpp code to print alternate nodes
#include
using namespace std;
/* link list node */
struct node
{
int data;
struct node* next;
};
/* function to get the alternate
nodes of the linked list */
void printalternatenode(struct node* head)
{
int count = 0;
while (head != null)
{
// when count is even print the nodes
if (count % 2 == 0)
cout << head->data << " ";
// count the nodes
count ;
// move on the next node.
head = head->next;
}
}
// function to push node at head
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;
}
// driver code
int main()
{
/* start with the empty list */
struct node* head = null;
/* use push() function to construct
the below list 8 -> 23 -> 11 -> 29 -> 12 */
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
printalternatenode(head);
return 0;
}
// this code is contributed by shubhamsingh10
c
// c code to print alternate nodes
#include
#include
/* link list node */
struct node {
int data;
struct node* next;
};
/* function to get the alternate
nodes of the linked list */
void printalternatenode(struct node* head)
{
int count = 0;
while (head != null) {
// when count is even print the nodes
if (count % 2 == 0)
printf(" %d ", head->data);
// count the nodes
count ;
// move on the next node.
head = head->next;
}
}
// function to push node at head
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;
}
// driver code
int main()
{
/* start with the empty list */
struct node* head = null;
/* use push() function to construct
the below list 8 -> 23 -> 11 -> 29 -> 12 */
push(&head, 12);
push(&head, 29);
push(&head, 11);
push(&head, 23);
push(&head, 8);
printalternatenode(head);
return 0;
}
java
// java code to print alternate nodes
class gfg
{
/* link list node */
static class node
{
int data;
node next;
};
/* function to get the alternate
nodes of the linked list */
static void printalternatenode( node head)
{
int count = 0;
while (head != null)
{
// when count is even print the nodes
if (count % 2 == 0)
system.out.printf(" %d ", head.data);
// count the nodes
count ;
// move on the next node.
head = head.next;
}
}
// function to push node at head
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;
}
// driver code
public static void main(string args[])
{
/* start with the empty list */
node head = null;
/* use push() function to con
the below list 8 . 23 . 11 . 29 . 12 */
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
printalternatenode(head);
}
}
// this code is contributed by arnab kundu
python3
# python3 code to print alternate nodes
# link list node
class node :
def __init__(self, data = none) :
self.data = data
self.next = none
# function to push node at head
def push(self, data) :
new = node(data)
new.next = self
return new
# function to get the alternate
# nodes of the linked list
def printalternatenode(self) :
head = self
while head and head.next != none :
print(head.data, end = " ")
head = head.next.next
# driver code
node = node()
# use push() function to construct
# the below list 8 -> 23 -> 11 -> 29 -> 12
node = node.push(12)
node = node.push(29)
node = node.push(11)
node = node.push(23)
node = node.push(8)
node.printalternatenode()
c
// c# code to print alternate nodes
using system;
class gfg
{
/* link list node */
public class node
{
public int data;
public node next;
};
/* function to get the alternate
nodes of the linked list */
static void printalternatenode( node head)
{
int count = 0;
while (head != null)
{
// when count is even print the nodes
if (count % 2 == 0)
console.write(" {0} ", head.data);
// count the nodes
count ;
// move on the next node.
head = head.next;
}
}
// function to push node at head
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;
}
// driver code
public static void main(string []args)
{
/* start with the empty list */
node head = null;
/* use push() function to con
the below list 8 . 23 . 11 . 29 . 12 */
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
printalternatenode(head);
}
}
// this code has been contributed by 29ajaykumar
输出:
8 11 12
**时间复杂度:o(n)
辅助空间:o(1)
**
提问者:govivace
如果您喜欢 geeksforgeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 geeksforgeeks pg电子试玩链接主页上,并帮助其他 geeks。
如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处