{"id":27692,"date":"2018-04-09T20:21:09","date_gmt":"2018-04-09T14:51:09","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27692"},"modified":"2018-09-11T19:41:16","modified_gmt":"2018-09-11T14:11:16","slug":"cpp-programming-compare-two-strings-represented-as-linked-lists","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/cpp-programming-compare-two-strings-represented-as-linked-lists\/","title":{"rendered":"Cpp programming-Compare two strings represented as linked lists"},"content":{"rendered":"<p>Given two linked lists, represented as linked lists (every character is a node in linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are same, 1 if first linked list is lexicographically greater, and -1 if second string is lexicographically greater.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input: list1 = g->e->e->k->s->a\r\n       list2 = g->e->e->k->s->b\r\nOutput: -1\r\n\r\nInput: list1 = g->e->e->k->s->a\r\n       list2 = g->e->e->k->s\r\nOutput: 1\r\n\r\nInput: list1 = g->e->e->k->s\r\n       list2 = g->e->e->k->s\r\nOutput: 0\r\n\r\n<\/pre>\n<p><strong>c++ programming<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20C%2B%2B%20program%20to%20compare%20two%20strings%20represented%20as%20linked%20%0A%2F%2F%20lists%0A%23include%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%20%20%0A%2F%2F%20Linked%20list%20Node%20structure%0Astruct%20Node%0A%7B%0A%20%20%20%20char%20c%3B%0A%20%20%20%20struct%20Node%20*next%3B%0A%7D%3B%0A%20%20%0A%2F%2F%20Function%20to%20create%20newNode%20in%20a%20linkedlist%0ANode*%20newNode(char%20c)%0A%7B%0A%20%20%20%20Node%20*temp%20%3D%20new%20Node%3B%0A%20%20%20%20temp-%3Ec%20%3D%20c%3B%0A%20%20%20%20temp-%3Enext%20%3D%20NULL%3B%0A%20%20%20%20return%20temp%3B%0A%7D%3B%0A%20%0Aint%20compare(Node%20*list1%2C%20Node%20*list2)%20%0A%7B%20%20%20%20%0A%20%20%20%20%2F%2F%20Traverse%20both%20lists.%20Stop%20when%20either%20end%20of%20a%20linked%20%0A%20%20%20%20%2F%2F%20list%20is%20reached%20or%20current%20characters%20don\u2019t%20match%0A%20%20%20%20while%20(list1%20%26%26%20list2%20%26%26%20list1-%3Ec%20%3D%3D%20list2-%3Ec)%20%0A%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20list1%20%3D%20list1-%3Enext%3B%0A%20%20%20%20%20%20%20%20list2%20%3D%20list2-%3Enext%3B%0A%20%20%20%20%7D%0A%20%20%20%20%20%0A%20%20%20%20%2F%2F%20%20If%20both%20lists%20are%20not%20empty%2C%20compare%20mismatching%0A%20%20%20%20%2F%2F%20%20characters%0A%20%20%20%20if%20(list1%20%26%26%20list2)%20%0A%20%20%20%20%20%20%20%20return%20(list1-%3Ec%20%3E%20list2-%3Ec)%3F%201%3A%20-1%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20either%20of%20the%20two%20lists%20has%20reached%20end%0A%20%20%20%20if%20(list1%20%26%26%20!list2)%20return%201%3B%0A%20%20%20%20if%20(list2%20%26%26%20!list1)%20return%20-1%3B%0A%20%20%20%20%20%0A%20%20%20%20%2F%2F%20If%20none%20of%20the%20above%20conditions%20is%20true%2C%20both%20%0A%20%20%20%20%2F%2F%20lists%20have%20reached%20end%20%0A%20%20%20%20return%200%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%0Aint%20main()%0A%7B%0A%20%20%20%20Node%20*list1%20%3D%20newNode(\u2018g\u2019)%3B%0A%20%20%20%20list1-%3Enext%20%3D%20newNode(\u2018e\u2019)%3B%0A%20%20%20%20list1-%3Enext-%3Enext%20%3D%20newNode(\u2018e\u2019)%3B%0A%20%20%20%20list1-%3Enext-%3Enext-%3Enext%20%3D%20newNode(\u2018k\u2019)%3B%0A%20%20%20%20list1-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%20newNode(\u2018s\u2019)%3B%0A%20%20%20%20list1-%3Enext-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%20newNode(\u2018b\u2019)%3B%0A%20%0A%20%20%20%20Node%20*list2%20%3D%20newNode(\u2018g\u2019)%3B%0A%20%20%20%20list2-%3Enext%20%3D%20newNode(\u2018e\u2019)%3B%0A%20%20%20%20list2-%3Enext-%3Enext%20%3D%20newNode(\u2018e\u2019)%3B%0A%20%20%20%20list2-%3Enext-%3Enext-%3Enext%20%3D%20newNode(\u2018k\u2019)%3B%0A%20%20%20%20list2-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%20newNode(\u2018s\u2019)%3B%0A%20%20%20%20list2-%3Enext-%3Enext-%3Enext-%3Enext-%3Enext%20%3D%20newNode(\u2018a\u2019)%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20compare(list1%2C%20list2)%3B%0A%20%20%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>1<\/pre>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Compare two strings represented as linked lists-Linked list<br \/>\nGiven two linked lists, represented as linked lists (every character is a node <\/p>\n","protected":false},"author":1,"featured_media":31266,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81987,81981,81979,81984,81980,81985,81983,81986,81982],"class_list":["post-27692","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-add-two-numbers-represented-by-linked-lists","tag-c-linked-list-string-example","tag-compare-two-linked-list-in-c","tag-compare-two-linked-lists-hackerrank-solution","tag-compare-two-linked-lists-java","tag-how-would-you-detect-a-loop-in-a-linked-list","tag-insert-string-into-linked-list-in-c","tag-merge-two-sorted-linked-lists","tag-string-linked-list-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27692"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27692\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31266"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}