Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
- Return a deep copy of the list i.e. the head of the copied linked list.
- The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.
- For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.
- The original list should be restored if modified.
- Input format:
- Next line contains X space separated integers, elements of the linked list.
- Next line contains X space separated integers, random pointers of each node in the linked list.
- Output format
- Return a deep copy of the list i.e. the head of the copied linked list.
- Sample Input 1
- 10 20
- 2 2
- Sample Output 1
- 10 20
- 20 20
- Explanation 1
- ========================
- View of the Linked list
- ========================
- {10.next} -> {20}
- and
- {20.next} -> {null}
- {10.random} -> {20}
- and
- {20.random} -> {20}
- Constraints
- 3 <= K <= N <= 105
- |A[i]| <= 109
- */
- /*
- class ListNode{
- constructor(val){
- this.val = val;
- this.next = null;
- }
- */
- /**
- * @param {ListNode} head1
- * @param {ListNode} head2
- * @return {ListNode}
- */
- function sumLists2(head1, head2) {
- let str1='',str2='';
- let pointer1=head1, pointer2=head2;
- let carry=0;
- let length1=0,length2=0;
- let result='';
- if(pointer1==null)
- return pointer2;
- if(pointer2==null)
- return pointer1
- while(pointer1!=null){
- str1=str1+pointer1.val.toString();
- length1++;
- pointer1=pointer1.next;
- }
- while(pointer2!=null){
- str2=str2+pointer2.val.toString();
- length2++;
- pointer2=pointer2.next;
- }
- // console.log(str1);
- let i=length1-1;
- let j=length2-1;
- while(i>=0&&j>=0){
- let currentVal=parseInt(str1[i])+parseInt(str2[j])+carry;
- carry=Math.floor(currentVal/10);
- currentVal=Math.floor(currentVal%10).toString();
- result=currentVal.toString()+result;
- i--;
- j--;
- }
- while(i>=0){
- // means str1 is remaining
- let currentVal=parseInt(str1[i])+carry;
- carry=Math.floor(currentVal/10);
- currentVal=Math.floor(currentVal%10).toString();
- result=currentVal.toString()+result;
- i--;
- }
- while(j>=0){
- // means str1 js remajnjng
- let currentVal=parseInt(str2[j])+carry;
- carry=Math.floor(currentVal/10);
- currentVal=Math.floor(currentVal%10).toString();
- result=currentVal.toString()+result;
- j--;
- }
- if(carry>0)
- result=carry.toString()+result;
- // console.log(result);
- // nopw simply convert resultant string into linkedList
- i=0;
- let currentPointer=null;
- let head=null;
- while(i<result.length){
- let newNode= new ListNode(parseInt(result[i]));
- if(currentPointer==null){
- currentPointer=newNode;
- head=currentPointer;
- }
- else{
- currentPointer.next=newNode;
- currentPointer=currentPointer.next;
- }
- i++;
- }
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement