Source : https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list
This challenge is part of a MyCodeSchool tutorial track and is accompanied by a video lesson.
If you're new to linked lists, this is a great exercise for learning about them. Given a pointer to the head node of a linked list, print its elements in order, one element per line. If the head pointer is null (indicating the list is empty), don’t print anything.
Input Format
The first line of input contains , the number of elements in the linked list.
The next lines contain one element each, which are the elements of the linked list.
Note: Do not read any input from stdin/console. Complete the printLinkedList function in the editor below.
Constraints
- , where is the element of the linked list.
Output Format
Print the integer data for each element of the linked list to stdout/console (e.g.: using printf, cout, etc.). There should be one element per line.
Sample Input
21613
Sample Output
1613
Explanation
There are two elements in the linked list. They are represented as 16 -> 13 -> NULL. So, the printLinkedList function should print 16 and 13 each in a new line.
Source : https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list
Solution
// Karthikalapati.blogspot.com | |
/* | |
Print elements of a linked list on console | |
Node is defined as | |
class Node { | |
int data; | |
Node next; | |
} | |
*/ | |
void Print(Node head) { | |
Node n = head; | |
while (n != null) { | |
System.out.println(n.data); | |
n = n.next; | |
} | |
} |
欢迎访问广发体育( GFTY SPORTS )官方下载中心,立即下载广发体育APP体验最新的足球篮球体育赛事、娱乐游戏,每天为您提供近千场精彩体育比分竞猜,互动娱乐在线直播平台,让玩家游戏的同时与女主播表演、参与互动游戏,广发全心全意为您提供最优质的服务。注册即赠送688,登录领取更多广发体育官方福利。广发体育APP官方下载网址: https://gf-ty.com
ReplyDelete#广发体育 #广发体育官网 #广发体育APP