Some LinkedList related problems

Problem Link

Sort the linked list according to the actual values:

/* The structure of the Linked list Node is as follows:

struct Node
{
    Node* next;
    int data;
}; */

void sortList(Node** head)
{
    // Your Code Here
    Node *tmp=(*head)->next;
    Node *prevHead;
    Node *tmp2=new Node;
    Node *node;
    bool f=0;
    while(tmp)
    {
        if(tmp->data<0)
        {
            if(f==0)
            prevHead=tmp;
            f=1;
            tmp2->data=tmp->data;
            tmp2->next=node;
            node=tmp2;
        }
        tmp=tmp->next;
    }
    if(prevHead)
    prevHead->next=(*head);
    (*head)=node;
}

ProblemLink

/* Link list node
struct Node
{
    int data;
    Node* next;
}*/

// This function should delete node from linked list. The function
// may assume that node exists in linked list and is not last node
void deleteNode(Node *node)
{
   // Your code here
   Node *tmp=node->next;
   node->data=tmp->data;
   node->next=tmp->next;
}

Problem Link

// the problem description is confusing
/*struct node
{
  int data;
  struct node *next;
  struct node *prev;
};
 */

void deleteNode(struct node **head_ref, struct node *del)
{
    if(del==*head_ref)
    {
        *head_ref=(*head_ref)->next;
        return;
    }
    node *tmp=(*head_ref)->next;
    while(tmp!=del)
    {
        tmp=tmp->next;
    }
    if(tmp==NULL)
    return;
    node *prv = tmp->prev;
    node *nxt= tmp->next;
    if(prv!=NULL)
    prv->next=nxt;
    if(nxt!=NULL)
    nxt->prev=prv;

}

Problem Link

/* The structure of linked list is the following

struct node
{
   int data;
   node* next;
};   */

//  function which splits the circular linked list.  head is pointer
// to head node of given lined list.  head1_ref1 and *head_ref2
// are pointers to head pointers of resultant two halves.

void splitList(struct node *head, struct node **head1_ref,
                                          struct node **head2_ref)
{
    // your code goes here
    *head1_ref=head;
    node *tmp=head;
	node *prev;
    tmp=head->next;
    int cnt=1;
    while(tmp!=head)
    {
        cnt+=1;
		prev=tmp;
        tmp=tmp->next;
    }
    int total;
    if(cnt%2)
    total=cnt/2+1;
    else
    total=cnt/2;

    tmp=head;
    for(int i=2;i<=total;i++)
    {
        tmp=tmp->next;
    }
	node *tmp2=tmp->next;
    tmp->next=(*head1_ref);
	(*head2_ref)=tmp2;
	prev->next=(*head2_ref);
}

Problem Link

/* The structure of the Linked list Node is as follows:
sort a list
struct Node
{
    Node* next;
    int data;
}; */

void sortList(Node** head)
{
    // Your Code Here
    Node *tmp=(*head);
    Node *prev=(*head);
    Node *bottom;
    Node *cur=prev->next;
    while(cur)
    {
        if(cur->data<0)
        {
            Node *tmp2=new Node;
            tmp2->data=cur->data;
            tmp2->next=tmp;
            tmp=tmp2;
            if(bottom==NULL)
            bottom=tmp;
            prev->next=cur->next;
            cur=cur->next;
        }
        else
        {
            prev=cur;
            cur=cur->next;
        }
    }
    // tmp2->data=100;
    if(bottom)
    { bottom->next=(*head);
     (*head)=tmp;
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *