File tree 5 files changed +178
-0
lines changed
5 files changed +178
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdlib.h>
2
+ #include <stdio.h>
3
+ typedef struct Node
4
+ {
5
+ int data ;
6
+ struct Node * next ;
7
+ }Node ;
8
+
9
+ Node * insert (Node * head ,int data )
10
+ { Node * nod =
11
+ nod = (Node * )malloc (sizeof (Node ));
12
+ nod -> data = data ;
13
+ if (head == NULL ) {head = nod ; return head ;}
14
+ Node * p ;
15
+ p = head ;
16
+ while (p -> next != NULL )
17
+ p = p -> next ;
18
+ p -> next = nod ;
19
+ return head ;
20
+ //Aditya Seth
21
+ }
22
+
23
+ void display (Node * head )
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+ using namespace std ;
4
+
5
+ class Node {
6
+ public:
7
+ int data;
8
+ Node *next;
9
+
10
+ Node (int d) {
11
+ data = d;
12
+ next = NULL ;
13
+ }
14
+ };
15
+
16
+ class Solution {
17
+ public:
18
+ Node *insert (Node *head, int data) {
19
+ if (head == NULL ) head = new Node (data);
20
+ else {
21
+ Node *curr = head;
22
+ while (curr->next ) curr = curr->next ;
23
+ curr->next = new Node (data);
24
+ }
25
+ return head;
26
+ }
27
+
28
+ void display (Node *head) {
29
+ Node *start = head;
30
+ while (start) {
31
+ cout << start->data << " " ;
32
+ start = start->next ;
33
+ }
34
+ }
35
+ };
36
+ // Aditya Seth
37
+ int main () {
38
+ Node *head = NULL ;
39
+ Solution mylist;
40
+ int T, data;
41
+ cin >> T;
42
+ while (T-- > 0 ) {
43
+ cin >> data;
44
+ head = mylist.insert (head, data);
45
+ }
46
+ mylist.display (head);
47
+ }
Original file line number Diff line number Diff line change
1
+ data class Node (var data : Int ) {
2
+ var next: Node ? = null
3
+ }
4
+
5
+ fun insert (head : Node ? , data : Int ): Node {
6
+ if (head == null ) {
7
+ return Node (data)
8
+ } else {
9
+ var curr: Node ? = head
10
+ while (curr?.next != null ) {
11
+ curr = curr.next
12
+ }
13
+ curr?.next = Node (data)
14
+ }
15
+ return head
16
+ }
17
+
18
+ fun display (head : Node ? ) {
19
+ var curr: Node ? = head
20
+ while (curr != null ) {
21
+ print (curr.data)
22
+ print (" " )
23
+ curr = curr.next
24
+ }
25
+ }
26
+
27
+ fun main (args : Array <String >) {
28
+ var head: Node ? = null
29
+ var N = Integer .parseInt(readLine())
30
+ while (N -- > 0 ) {
31
+ val element = Integer .parseInt(readLine())
32
+ head = insert(head, element)
33
+ }
34
+ display(head)
35
+ }
Original file line number Diff line number Diff line change
1
+ class Node :
2
+ def __init__ (self , data ):
3
+ self .data = data
4
+ self .next = None
5
+
6
+
7
+ class Solution :
8
+ def display (self , head ):
9
+ current = head
10
+ while current :
11
+ print (current .data , end = ' ' )
12
+ current = current .next
13
+
14
+ def insert (self , head , data ):
15
+ if head is None :
16
+ head = Node (data )
17
+ else :
18
+ curr = head
19
+ while curr .next :
20
+ curr = curr .next
21
+ curr .next = Node (data )
22
+ return head
23
+
24
+ #Aditya Seth
25
+ mylist = Solution ()
26
+ T = int (input ())
27
+ head = None
28
+ for i in range (T ):
29
+ data = int (input ())
30
+ head = mylist .insert (head , data )
31
+ mylist .display (head )
Original file line number Diff line number Diff line change
1
+ class Node
2
+
3
+ attr_accessor :data , :next
4
+
5
+ def initialize data
6
+ @data = data
7
+ @next = nil
8
+ end
9
+ end
10
+
11
+ class Solution
12
+ def insert ( head , value )
13
+ #Aditya Seth
14
+ if head . nil?
15
+ head = Node . new value
16
+ else
17
+ curr = head
18
+ until ( curr . next . nil? ) do
19
+ curr = curr . next
20
+ end
21
+ curr . next = Node . new value
22
+ end
23
+
24
+ head
25
+ end
26
+ def display ( head )
27
+ current = head
28
+ while current
29
+ print current . data , " "
30
+ current = current . next
31
+ end
32
+ end
33
+ end
34
+
35
+ mylist = Solution . new
36
+ head = nil
37
+ T = gets . to_i
38
+ for i in 1 ..T
39
+ data = gets . to_i
40
+ head = mylist . insert ( head , data )
41
+ end
42
+ mylist . display ( head )
You can’t perform that action at this time.
0 commit comments