-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathsolution.java
155 lines (140 loc) · 5.61 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.io.IOException;
import java.util.Scanner;
public class LinkedListEasy1 {
//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 insert Node at Head
static SinglyLinkedListNode insertNodeAtHead(SinglyLinkedListNode llist, int data) {
SinglyLinkedListNode temp = new SinglyLinkedListNode(data);
//If head is null simply point head to temp and return head
if (llist == null) {
llist = temp;
return llist;
}
//Point next of the new node created to head
temp.next = llist;
//Point head to temp
llist = temp;
//Return the head
return llist;
}
//Member method to insert a Node at a specific Position
static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode head, int data, int position) {
SinglyLinkedListNode nodeToInsert = new SinglyLinkedListNode(data);
//Empty List - Returned newly created node or null
if (head==null){
return nodeToInsert;
}
//Inserting a Node ahead of the List
if (position == 0){
nodeToInsert.next = head;
return nodeToInsert;
}
//Create a copy of Head Node
SinglyLinkedListNode curr = head;
//Traverse the Singly Linked List to 1 Position Prior
//Stop traversing if you reached the end of the List
int currPosition = 0;
while (currPosition < position -1 && curr.next != null){
curr = curr.next;
currPosition++;
}
//Inserting a Node in-between a List or at the end of of a List
nodeToInsert.next = curr.next;
curr.next = nodeToInsert;
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 for adding node at head of Linked list
llistCount = scanner.nextInt();
for (int i = 0; i < llistCount; i++) {
int llistItem = scanner.nextInt();
SinglyLinkedListNode llist_head = insertNodeAtHead(llist.head, llistItem);
llist.head = llist_head;
}
//Code for adding node at a specific position
int data = scanner.nextInt();
int position = scanner.nextInt();
llist.head = insertNodeAtPosition(llist.head, data, position);
//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();
}
}