Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f5ed84c

Browse files
added RemoveDuplicates, insert Value, delete nth node from last methods
1 parent fba4ddd commit f5ed84c

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

‎.idea/runConfigurations.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎DSA_College/LinkList/SinglyLinkedList.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package DSA_College.LinkList;
22

3+
import java.util.HashMap;
4+
35
public class SinglyLinkedList {
46
private ListNode head;
57

@@ -55,6 +57,16 @@ else if (index > size() || index < 0) {
5557
}
5658
}
5759

60+
// insert a node in sorted order
61+
public void insertValue(int data) {
62+
ListNode n = head, node = new ListNode(data);
63+
while (n.next != null && n.data < data) {
64+
n = n.next;
65+
}
66+
node.next = n.next;
67+
n.next = node;
68+
}
69+
5870
// delete node at the beginning
5971
public int deleteFirst() {
6072
if (head == null) {
@@ -87,6 +99,7 @@ else if (pos == size())
8799
}
88100
return -1;
89101
}
102+
90103
// delete a node at the last
91104
public int delete() {
92105
if (size() == 1) {
@@ -124,6 +137,11 @@ public int middle() {
124137
return slow.data;
125138
}
126139

140+
// Delete nth node from last
141+
public int deleteNthLast(int index) {
142+
return delete(size() - index - 1);
143+
}
144+
127145
// rotate a linked List
128146
public ListNode rotateRight(ListNode head, int k) {
129147
ListNode last = null, crr = head, node;
@@ -145,6 +163,19 @@ public ListNode rotateRight(ListNode head, int k) {
145163
return head;
146164
}
147165

166+
// remove the duplicate nodes from sorted linked list
167+
public void removeDuplicates() {
168+
ListNode n = head;
169+
HashMap<Integer, Integer> h = new HashMap<>();
170+
while (n != null && n.next != null) {
171+
if (n.data == n.next.data)
172+
n.next = n.next.next;
173+
else {
174+
n = n.next;
175+
}
176+
}
177+
}
178+
148179
public static void main(String[] args) throws Exception {
149180
SinglyLinkedList sll = new SinglyLinkedList();
150181
// sll.head = new ListNode(10);
@@ -201,7 +232,18 @@ public static void main(String[] args) throws Exception {
201232

202233
System.out.println("Middle Of the Linked List => " + sll.middle() + "\n");
203234

204-
System.out.println(sll.rotateRight(sll.head, 2));
235+
// System.out.println(sll.rotateRight(sll.head, 2));
236+
// sll.print();
237+
238+
System.out.println("Deleted from last => " + sll.deleteNthLast(2));
239+
sll.print();
240+
241+
sll.add(8);
242+
sll.removeDuplicates();
243+
sll.print();
244+
System.out.println();
245+
246+
sll.insertValue(7);
205247
sll.print();
206248
}
207249
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package PraticeQues.LInkedList;
2+
3+
public class RemoveDuplicates {
4+
5+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /