Re: [algogeeks] Re: Problems on Linked List
@mohit : +1 On Sat, Aug 13, 2011 at 9:20 AM, Raghavan its...@gmail.com wrote: First question: - .Read the data from the first list and put it in a stack - Traverse the next list and compare by reading elements from the stack - This would solve it Second question: - Take an list like 1-2--3-4 - If you are given with 2, juz copy the value and reference of next node(i.e : 3) into 2 and delete the node 3 this would solve it Thanks. On Thu, Aug 11, 2011 at 10:21 AM, Abhishek gupta mailatabhishekgu...@gmail.com wrote: Q2). i think for second question it will be enough to just swap the data of current node to next node, and delete the next node. it will be like, //for swap int temp=current-data; current-data=current-next-data; current-next-data=temp; //for delete struct node *temp; temp=current-next; current-next=current-next-next; free(temp); i think it will be enough even in case of last node. correct me if i am wrong. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/N1elA8-W-iUJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Thanks and regards, Raghavan.K.L http://in.linkedin.com/in/raghavankl -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Problems on Linked List
folks .. for second problem .. the mentioned algo doesn't work for last node.. I mean copying data of next node and aalso making nxt-nxt as nxt node.. and deleting the next node.. in case of last node.. how can we delete the last node being on that node? even though if we use free (node) ... the prev node-next value doesn't become NULL .. It will contains some garbage.. On Sat, Aug 13, 2011 at 11:34 AM, aditya kumar aditya.kumar130...@gmail.com wrote: @mohit : +1 On Sat, Aug 13, 2011 at 9:20 AM, Raghavan its...@gmail.com wrote: First question: - .Read the data from the first list and put it in a stack - Traverse the next list and compare by reading elements from the stack - This would solve it Second question: - Take an list like 1-2--3-4 - If you are given with 2, juz copy the value and reference of next node(i.e : 3) into 2 and delete the node 3 this would solve it Thanks. On Thu, Aug 11, 2011 at 10:21 AM, Abhishek gupta mailatabhishekgu...@gmail.com wrote: Q2). i think for second question it will be enough to just swap the data of current node to next node, and delete the next node. it will be like, //for swap int temp=current-data; current-data=current-next-data; current-next-data=temp; //for delete struct node *temp; temp=current-next; current-next=current-next-next; free(temp); i think it will be enough even in case of last node. correct me if i am wrong. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/N1elA8-W-iUJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Thanks and regards, Raghavan.K.L http://in.linkedin.com/in/raghavankl -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Thx, --Gopi -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Problems on Linked List
yes you are right Thank you, Siddharam On Thu, Aug 11, 2011 at 10:21 AM, Abhishek gupta mailatabhishekgu...@gmail.com wrote: Q2). i think for second question it will be enough to just swap the data of current node to next node, and delete the next node. it will be like, //for swap int temp=current-data; current-data=current-next-data; current-next-data=temp; //for delete struct node *temp; temp=current-next; current-next=current-next-next; free(temp); i think it will be enough even in case of last node. correct me if i am wrong. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/N1elA8-W-iUJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Problems on Linked List
Q2). i think for second question it will be enough to just swap the data of current node to next node, and delete the next node. it will be like, //for swap int temp=current-data; current-data=current-next-data; current-next-data=temp; //for delete struct node *temp; temp=current-next; current-next=current-next-next; free(temp); i think it will be enough even in case of last node. correct me if i am wrong. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/N1elA8-W-iUJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Problems on Linked List
First question: - .Read the data from the first list and put it in a stack - Traverse the next list and compare by reading elements from the stack - This would solve it Second question: - Take an list like 1-2--3-4 - If you are given with 2, juz copy the value and reference of next node(i.e : 3) into 2 and delete the node 3 this would solve it Thanks. On Thu, Aug 11, 2011 at 10:21 AM, Abhishek gupta mailatabhishekgu...@gmail.com wrote: Q2). i think for second question it will be enough to just swap the data of current node to next node, and delete the next node. it will be like, //for swap int temp=current-data; current-data=current-next-data; current-next-data=temp; //for delete struct node *temp; temp=current-next; current-next=current-next-next; free(temp); i think it will be enough even in case of last node. correct me if i am wrong. -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/N1elA8-W-iUJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Thanks and regards, Raghavan.K.L http://in.linkedin.com/in/raghavankl -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Problems on Linked List
hey guys , can't it be like this without reversing list- int rec_iterate(Node head1,Node *head2) { if(head1 ==NULL ) return 1; if(rec_iterate(head1-n,head2) == 0) return 0; if (head1-value == (*head2)-value) { *head2=(*head2)-next; return 1; } else return 0; } provided lists are of same length. On Thu, Aug 11, 2011 at 1:30 AM, Don dondod...@gmail.com wrote: Q1: The function below reverses a linked list in place. Call it on one of the lists, compare the resulting list to the other list. Then call it again to put the list back in its original order. list Reverse(list head) { list T, prv, nxt; prv = head; for(T = head-next; T; T = nxt) { nxt = T-next; T-next = prv; prv = T; T = nxt; } head-next = 0; return prv; } Q2: delete(node *d) { if (d-next) { node nxt = d-next; d-value = nxt-value; d-next = nxt-next; free nxt; } else { for(node p = head; p; p = p-next) if (p-next == d) { p-next = 0; free d; } } } On Aug 10, 1:14 pm, Piyush Kapoor pkjee2...@gmail.com wrote: Q1)Two linked Lists are given,i.e,their head pointers are given,and the problem is to check if the second one is reverse of the first one.Give the most efficient algo for it. Q2)A linked list is given,and one of its nodes is given.The problem is to delete the given node from the linked list.(The head node is not given). (In both of the above cases,the linked lists are singly linked lists.) -- *Regards,* *Piyush Kapoor,* *2nd year,CSE IT-BHU* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- *MOHIT VERMA* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: Problems on Linked List
Q1: The function below reverses a linked list in place. Call it on one of the lists, compare the resulting list to the other list. Then call it again to put the list back in its original order. list Reverse(list head) { list T, prv, nxt; prv = head; for(T = head-next; T; T = nxt) { nxt = T-next; T-next = prv; prv = T; T = nxt; } head-next = 0; return prv; } Q2: delete(node *d) { if (d-next) { node nxt = d-next; d-value = nxt-value; d-next = nxt-next; free nxt; } else { for(node p = head; p; p = p-next) if (p-next == d) { p-next = 0; free d; } } } On Aug 10, 1:14 pm, Piyush Kapoor pkjee2...@gmail.com wrote: Q1)Two linked Lists are given,i.e,their head pointers are given,and the problem is to check if the second one is reverse of the first one.Give the most efficient algo for it. Q2)A linked list is given,and one of its nodes is given.The problem is to delete the given node from the linked list.(The head node is not given). (In both of the above cases,the linked lists are singly linked lists.) -- *Regards,* *Piyush Kapoor,* *2nd year,CSE IT-BHU* -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.