Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct node* copyList()
- {
- //check if empty
- if(head==NULL){
- return NULL;}
- //create head
- struct node* p_head=head;//pointer to original head
- struct node* newHead = NULL; //head of new list
- struct node* p_newHead=newHead;//pointer to new head
- while(p_head!=NULL)
- int data=p_head->data;
- m_appendNode(data, p_newHead);
- p_head=p_head->next;
- }
- return newHead;
- }
- void m_appendNode(int data, struct node *newHead)
- {
- struct node* current=newHead;
- if(newHead!=NULL){
- while((current->next)!=NULL){
- current = current->next;
- }
- current->next=malloc(sizeof(struct node));
- current->next->data=data;
- current->next->next=NULL;
- }
- else{
- struct node* newnode=malloc(sizeof(struct node));
- newnode->data=data;
- newHead=newnode;
- newnode->next=NULL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement