{"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-&gt;e-&gt;e-&gt;k-&gt;s-&gt;a\r\n       list2 = g-&gt;e-&gt;e-&gt;k-&gt;s-&gt;b\r\nOutput: -1\r\n\r\nInput: list1 = g-&gt;e-&gt;e-&gt;k-&gt;s-&gt;a\r\n       list2 = g-&gt;e-&gt;e-&gt;k-&gt;s\r\nOutput: 1\r\n\r\nInput: list1 = g-&gt;e-&gt;e-&gt;k-&gt;s\r\n       list2 = g-&gt;e-&gt;e-&gt;k-&gt;s\r\nOutput: 0\r\n\r\n<\/pre>\n<p><strong>c++ programming<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to compare two strings represented as linked <br\/>\/\/ lists<br\/>#include&lt;bits\/stdc++.h&gt;<br\/>using namespace std;<br\/>  <br\/>\/\/ Linked list Node structure<br\/>struct Node<br\/>{<br\/>    char c;<br\/>    struct Node *next;<br\/>};<br\/>  <br\/>\/\/ Function to create newNode in a linkedlist<br\/>Node* newNode(char c)<br\/>{<br\/>    Node *temp = new Node;<br\/>    temp-&gt;c = c;<br\/>    temp-&gt;next = NULL;<br\/>    return temp;<br\/>};<br\/> <br\/>int compare(Node *list1, Node *list2) <br\/>{    <br\/>    \/\/ Traverse both lists. Stop when either end of a linked <br\/>    \/\/ list is reached or current characters don&#039;t match<br\/>    while (list1 &amp;&amp; list2 &amp;&amp; list1-&gt;c == list2-&gt;c) <br\/>    {         <br\/>        list1 = list1-&gt;next;<br\/>        list2 = list2-&gt;next;<br\/>    }<br\/>     <br\/>    \/\/  If both lists are not empty, compare mismatching<br\/>    \/\/  characters<br\/>    if (list1 &amp;&amp; list2) <br\/>        return (list1-&gt;c &gt; list2-&gt;c)? 1: -1;<br\/> <br\/>    \/\/ If either of the two lists has reached end<br\/>    if (list1 &amp;&amp; !list2) return 1;<br\/>    if (list2 &amp;&amp; !list1) return -1;<br\/>     <br\/>    \/\/ If none of the above conditions is true, both <br\/>    \/\/ lists have reached end <br\/>    return 0;<br\/>}<br\/> <br\/>\/\/ Driver program<br\/>int main()<br\/>{<br\/>    Node *list1 = newNode(&#039;g&#039;);<br\/>    list1-&gt;next = newNode(&#039;e&#039;);<br\/>    list1-&gt;next-&gt;next = newNode(&#039;e&#039;);<br\/>    list1-&gt;next-&gt;next-&gt;next = newNode(&#039;k&#039;);<br\/>    list1-&gt;next-&gt;next-&gt;next-&gt;next = newNode(&#039;s&#039;);<br\/>    list1-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(&#039;b&#039;);<br\/> <br\/>    Node *list2 = newNode(&#039;g&#039;);<br\/>    list2-&gt;next = newNode(&#039;e&#039;);<br\/>    list2-&gt;next-&gt;next = newNode(&#039;e&#039;);<br\/>    list2-&gt;next-&gt;next-&gt;next = newNode(&#039;k&#039;);<br\/>    list2-&gt;next-&gt;next-&gt;next-&gt;next = newNode(&#039;s&#039;);<br\/>    list2-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(&#039;a&#039;);<br\/> <br\/>    cout &lt;&lt; compare(list1, list2);<br\/>  <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>1<\/pre>\n<p>&nbsp;<\/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}]}}