Navigation

    ask avan logo
    • Register
    • Login
    • Search
    • Categories
    • Unsolved
    • Solved
    1. Home
    2. Devatha Naga Puneeth
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 1
    • Best 0
    • Groups 0

    Devatha Naga Puneeth

    @Devatha Naga Puneeth

    0
    Reputation
    1
    Profile views
    1
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    Devatha Naga Puneeth Follow

    Latest posts made by Devatha Naga Puneeth

    • Getting segmentation error in merging two sorted linked list

      Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list.
      Note: It is strongly recommended to do merging in-place using O(1) extra space.
      QUESTION URL:https://practice.geeksforgeeks.org/problems/merge-two-sorted-linked-lists/1

      My solution in C++:(This is only the main code )

      
      struct Node {
        int data;
        struct Node *next;
        
        Node(int x) {
          data = x;
          next = NULL;
        }
      };
      
      
      Node* sortedMerge(Node* head_A, Node* head_B)  
      {  
          Node* third = NULL;
          Node* last  = NULL;
          if(head_A->data < head_B->data)
          {
              third  = head_A;
              last   = head_A;
              head_A = head_A->next;
              third->next = NULL;
          }
          else
          {
              third  = head_B;
              last   = head_B;
              head_B = head_B->next;
              third->next = NULL;
          }
          
          
          while(head_A && head_B)
          {
              if(head_A->data < head_B->data)
              {
                  last->next = head_A;
                  last = head_A;
                  head_A = head_A->next;
                  last->next = NULL;
              }
              if(head_A->data >  head_B->data)
              {
                  last->next = head_B;
                  last = head_B;
                  head_B = head_B->next;
                  last->next = NULL;
              }
              
          }
          if(head_A)
              {
                  last->next = head_A;
              }
              if(head_B)
              {
                  last->next = head_B;
              }
          return third;
      }  
      

      I am getting segmentation error when i was submitted and i want know the test case where i am getting segmentation error

      posted in C++
      Devatha Naga Puneeth
      Devatha Naga Puneeth