Advertisement
satishfrontenddev4

Untitled

Jan 5th, 2024
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Given a pointer to a node in a sorted circular singly linked list( sorted in ascending order), write a function to insert a value K into the list such that the linked list still remains a sorted circular list.
  3.  
  4.  
  5. Note1: If the given pointer is null i.e. empty list, create a circular list with a new node and return the reference to the new node. Otherwise,return the initial input pointer given.
  6.  
  7.  
  8. Note2: The list could have duplicate values. You can insert the new value in any place which will keep the list sorted.
  9.  
  10. Input Format
  11. First line contains N, the number of nodes in the linked list.
  12.  
  13. The next line contains N space-separated integers, representing the nodes of the circular linked list.
  14.  
  15. The last line contains a single integer K, denoting the value that is to be inserted.
  16.  
  17. Output Format
  18. Return the original pointer given as an argument, after modifying the list by inserting the value. The output prints the modified linked list starting from the returned node.
  19.  
  20. Constraints
  21. 0 <= N <= 10^5
  22.  
  23. 0 <= value <= 10^9
  24.  
  25. 0 <= K <= 10^9
  26.  
  27. Sample Input 1
  28. 7
  29.  
  30. 4 5 9 10 0 1 2
  31.  
  32. 7
  33.  
  34. Sample Output 1
  35. 4 5 7 9 10 0 1 2
  36.  
  37. Explanation 1
  38. The input is a sorted circular linked list and the given pointer is a reference to node [4]. A new node with [7] has to be inserted between [5] and [9] for the list to remain sorted. Return node [4] which is the input pointer given.
  39.  
  40. Sample Input 2
  41. 0
  42.  
  43. 4
  44.  
  45. Sample Output 2
  46. 4
  47.  
  48. Explanation 2
  49. The input is a sorted circular linked list which is empty. Create a circular linked list with a new node [4] and return the new node.
  50. */
  51.  
  52. /*
  53. class ListNode{
  54.     constructor(val){
  55.         this.val = val;
  56.         this.next = null;
  57.     }
  58. */
  59. /**
  60.  * @param {ListNode} head
  61.  * @param {number} insertVal
  62.  * @return {ListNode}
  63.  */
  64.  
  65. /*The approach you've described is for performing a sorted insert into a circular linked list. Let's break down each case and provide some intuition for each scenario:
  66.  
  67. **1) Linked List is empty:**
  68.    - In this case, you are inserting a new node into an empty circular linked list.
  69.    - You create a new node (`new_node`) and make it point to itself, creating a self-loop.
  70.    - You then update the head pointer to point to the new node, making it the only node in the list.
  71.    - Intuition: Since the list is empty, the new node becomes both the first and last node in the circular list, and it points to itself.
  72.  
  73. **2) New node is to be inserted just before the head node:**
  74.    - Here, you want to insert a new node just before the current head node.
  75.    - You need to find the last node in the circular list, which can be done by traversing the list until you encounter the node whose `next` points to the current head.
  76.    - You update the `next` of the last node to point to the new node, effectively connecting the last node to the new node.
  77.    - You then update the `next` of the new node to point to the current head, making it the new head.
  78.    - Intuition: You are essentially moving the head to the new node and connecting the last node to it, creating a new circular arrangement.
  79.  
  80. **3) New node is to be inserted somewhere after the head:**
  81.    - In this case, you want to insert a new node somewhere after the current head node while maintaining the sorted order.
  82.    - You start by traversing the list until you find the node after which you want to insert the new node. This involves checking the value of the `next` node in the traversal.
  83.    - Once you've found the appropriate insertion point, you update the `next` of the new node to point to the node that came after it.
  84.    - Then, you update the `next` of the node you found in the traversal to point to the new node, effectively inserting it into the list.
  85.    - Intuition: You are finding the correct position for insertion and adjusting the pointers of the surrounding nodes to include the new node while maintaining the sorted order.
  86.  
  87. In all cases, the key is to manipulate the `next` pointers of the nodes to insert the new node correctly and maintain the circular structure of the linked list. The approach ensures that the list remains sorted after the insertion. */
  88. function insertIntoSortedCircularList(head, insertVal) {
  89.     let newNode=new ListNode(insertVal);
  90.     let curr=head;
  91.     let prev=null;
  92.     // if list is empty
  93.     if(head==null)
  94.     {
  95.         newNode.next=newNode;
  96.         head=newNode;
  97.         return head;
  98.     }
  99.  
  100.     // if newnode is to be inserted at front of cll
  101.     else if(curr.val>=insertVal){
  102.         while(curr.next!=head){
  103.                 curr=curr.next;
  104.         }
  105.         curr.next=newNode;
  106.         newNode.next=head;
  107.         head=newNode;
  108.         return head
  109.     }
  110.     else{
  111.         //  if newnode is to be inserted somewhere in between the CLL
  112.         let prev=null;
  113.         while(curr.next!=head&&curr.val<=insertVal){
  114.             prev=curr;
  115.             curr=curr.next;
  116.         }
  117.         // check if newNode is to be inserted at the end
  118.             newNode.next=prev.next;
  119.             prev.next=newNode;
  120.  
  121.         return head;
  122.     }
  123.  
  124.     return head;
  125.  
  126.    
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement