Doubly linked list Insertion at given position GFG Problem
Input: head = [5,2,13,3,8]
Output: [13,8]
Explanation: The nodes that should be removed are 5, 2 and 3.
- Node 13 is to the right of node 5.
- Node 13 is to the right of node 2.
- Node 8 is to the right of node 3.
Example 2:
Input: head = [1,1,1,1]
Output: [1,1,1,1]
Explanation: Every node has value 1, so no nodes are removed.Given a doubly-linked list, a position p, and an integer x. The task is to add a new node with value x at the position just after pth node in the doubly linked list and return the head of the updated list.
Examples:
Input: LinkedList: 2<->4<->5, p = 2, x = 6
Output: 2<->4<->5<->6
Explanation: p = 2, and x = 6. So, 6 is inserted after p, i.e, at position 2 (0-based indexing).
Input: LinkedList: 1<->2<->3<->4, p = 0, x = 44
Output: 1<->44<->2<->3<->4
Explanation: p = 0, and x = 44 . So, 44 is inserted after p, i.e, at position 0 (0-based indexing).
//{ Driver Code Starts
import java.io.*;
import java.util.*;
class Node {
int data;
Node next;
Node prev;
Node(int data) {
this.data = data;
next = prev = null;
}
}
class DLinkedList {
Node newNode(Node head, int data) {
Node n = new Node(data);
if (head == null) {
head = n;
return head;
}
head.next = n;
n.prev = head;
head = n;
return head;
}
void printList(Node node) {
Node temp = node;
while (temp.next != null) {
temp = temp.next;
}
while (temp.prev != null) {
temp = temp.prev;
}
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String args[]) throws IOException {
DLinkedList DLL = new DLinkedList();
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t > 0) {
Node temp;
Node head = null;
Node root = null;
String str[] = read.readLine().trim().split(" ");
int n = str.length;
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(str[i]);
head = DLL.newNode(head, x);
if (root == null) root = head;
}
head = root;
String str2[] = read.readLine().trim().split(" ");
int pos = Integer.parseInt(str2[0]);
int data = Integer.parseInt(str2[1]);
Solution g = new Solution();
head = g.addNode(head, pos, data);
DLL.printList(head);
while (head.next != null) {
temp = head;
head = head.next;
}
t--;
System.out.println("~");
}
}
}
// } Driver Code Ends
/* Structure of Doubly Linked List
class Node
{
int data;
Node next;
Node prev;
Node(int data)
{
this.data = data;
next = prev = null;
}
}*/
class Solution {
// Function to insert a new node at given position in doubly linked list.
Node addNode(Node head, int p, int x) {
// Your code here
Node newnode = new Node(x);
Node temp = head;
// Traverse to (p)-th node
for (int i = 0; i < p; i++) {
if (temp == null) return head; // If position is out of bounds
temp = temp.next;
}
// Inserting newnode after 'temp'
newnode.next = temp.next;
newnode.prev = temp;
if (temp.next != null) {
temp.next.prev = newnode; // Avoid NullPointerException
}
temp.next = newnode;
return head;
}
}
Comments
Post a Comment