-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday25.java
More file actions
120 lines (98 loc) · 2.68 KB
/
day25.java
File metadata and controls
120 lines (98 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//ques1:237. Delete Node in a Linked List
//link:https://leetcode.com/problems/delete-node-in-a-linked-list/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val=node.next.val;
node.next=node.next.next;
}
}
//ques2:Linked List Insertion At End
//link:https://www.geeksforgeeks.org/problems/linked-list-insertion-1587115620/0?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=linked-list-insertion
//{ Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
// } Driver Code Ends
/*
class Node{
int data;
Node next;
Node(int x){
data = x;
next = null;
}
}
*/
class Solution {
// Function to insert a node at the end of the linked list.
Node insertAtEnd(Node head, int x) {
// code here
Node nn= new Node(x);
Node cur=head;
if(head==null){
return nn;
}
while(cur.next!=null){
cur=cur.next;
}
cur.next=nn;
return head;
}
}
//{ Driver Code Starts.
public class GFG {
static void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
List<Integer> arr = new ArrayList<>();
String input = br.readLine().trim();
if (!input.isEmpty()) {
String[] numbers = input.split("\\s+");
for (String num : numbers) {
if (!num.isEmpty()) {
arr.add(Integer.parseInt(num));
}
}
}
int x = Integer.parseInt(br.readLine().trim());
Node head = null;
if (!arr.isEmpty()) {
head = new Node(arr.get(0));
Node tail = head;
for (int i = 1; i < arr.size(); ++i) {
tail.next = new Node(arr.get(i));
tail = tail.next;
}
}
Solution ob = new Solution();
Node ans = ob.insertAtEnd(head, x);
printList(ans);
System.out.println("~");
}
}
}
// } Driver Code Ends