forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.java
101 lines (89 loc) · 3.81 KB
/
solution.java
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
import java.io.IOException;
import java.util.Scanner;
public class solution {
//Class for Node
static class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;
public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}
//Class for Linked List
static class SinglyLinkedList {
public SinglyLinkedListNode head;
public SinglyLinkedList() {
this.head = null;
}
}
//Member method to print the data of the nodes of singlyLinkedList
public static void printSinglyLinkedList(SinglyLinkedListNode node) throws IOException {
while (node != null) {
System.out.print(node.data + " -> ");
node = node.next;
}
System.out.println("NULL");
}
//Member method to insert node at tail
static SinglyLinkedListNode insertNodeAtTail(SinglyLinkedListNode head, int data) {
//Create the node to be inserted in the Linked List
SinglyLinkedListNode temp = new SinglyLinkedListNode(data);
//If head is null simply point head to temp and return head
if (head == null) {
head = temp;
return head;
}
//If head is not null iterate through the Linked List using a temp Node: curr
SinglyLinkedListNode curr = head;
//Exit the while loop when curr.next points to null
while (curr.next != null) {
curr = curr.next;
}
//Add the temp node at curr.next
curr.next = temp;
//Return the head
return head;
}
//Member method to delete a Node at a specific position
static SinglyLinkedListNode deleteNode(SinglyLinkedListNode head, int position) {
//Iterate through the list if head is not null
if(head != null){
//Create a copy of head
SinglyLinkedListNode temp;
temp = head;
//If position of node to be deleted is 0 simply return head.next
//that is the element to which head is pointing
if(position == 0)
head = head.next;
//If position is not 1 than iterate to position - 1
else {
for(int i=0; i<position-1; i++)
temp = temp.next;
//Now remove the next node by derefrencing it
// that is pointing the next of current node to next of next of current node
temp.next = (temp.next).next;
}
}
return head;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
//Creating Singly Linked List
SinglyLinkedList llist = new SinglyLinkedList();
//Code for adding node at tail of Linked List
int llistCount = scanner.nextInt();
for (int i = 0; i < llistCount; i++) {
int llistItem = scanner.nextInt();
SinglyLinkedListNode llist_head = insertNodeAtTail(llist.head, llistItem);
llist.head = llist_head;
}
//Code to delete a node at a specific position
position = scanner.nextInt();
llist.head = deleteNode(llist.head, position);
//Call to Method to print out the Linked List
printSinglyLinkedList(llist.head);
scanner.close();
}
}
}