{"id":27711,"date":"2018-04-09T20:22:54","date_gmt":"2018-04-09T14:52:54","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27711"},"modified":"2018-09-11T19:27:25","modified_gmt":"2018-09-11T13:57:25","slug":"java-programming-compare-two-strings-represented-linked-lists","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-compare-two-strings-represented-linked-lists\/","title":{"rendered":"Java 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>Java programming<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20compare%20two%20strings%20represented%20as%20a%20linked%20list%0A%20%0A%2F%2F%20Linked%20List%20Class%0Aclass%20LinkedList%20%7B%0A%20%0A%20%20%20%20Node%20head%3B%20%20%2F%2F%20head%20of%20list%0A%20%20%20%20static%20Node%20a%2C%20b%3B%0A%20%0A%20%20%20%20%2F*%20Node%20Class%20*%2F%0A%20%20%20%20static%20class%20Node%20%7B%0A%20%0A%20%20%20%20%20%20%20%20char%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Constructor%20to%20create%20a%20new%20node%0A%20%20%20%20%20%20%20%20Node(char%20d)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20int%20compare(Node%20node1%2C%20Node%20node2)%20%7B%0A%20%0A%20%20%20%20%20%20%20%20if%20(node1%20%3D%3D%20null%20%26%26%20node2%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20while%20(node1%20!%3D%20null%20%26%26%20node2%20!%3D%20null%20%26%26%20node1.data%20%3D%3D%20node2.data)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20node1%20%3D%20node1.next%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20node2%20%3D%20node2.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20if%20the%20list%20are%20diffrent%20in%20size%0A%20%20%20%20%20%20%20%20if%20(node1%20!%3D%20null%20%26%26%20node2%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20(node1.data%20%3E%20node2.data%20%3F%201%20%3A%20-1)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20if%20either%20of%20the%20list%20has%20reached%20end%0A%20%20%20%20%20%20%20%20if%20(node1%20!%3D%20null%20%26%26%20node2%20%3D%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20if%20(node1%20%3D%3D%20null%20%26%26%20node2%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%0A%20%20%20%20%20%20%20%20LinkedList%20list%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20Node%20result%20%3D%20null%3B%0A%20%0A%20%20%20%20%20%20%20%20list.a%20%3D%20new%20Node(\u2018g\u2019)%3B%0A%20%20%20%20%20%20%20%20list.a.next%20%3D%20new%20Node(\u2018e\u2019)%3B%0A%20%20%20%20%20%20%20%20list.a.next.next%20%3D%20new%20Node(\u2018e\u2019)%3B%0A%20%20%20%20%20%20%20%20list.a.next.next.next%20%3D%20new%20Node(\u2018k\u2019)%3B%0A%20%20%20%20%20%20%20%20list.a.next.next.next.next%20%3D%20new%20Node(\u2018s\u2019)%3B%0A%20%20%20%20%20%20%20%20list.a.next.next.next.next.next%20%3D%20new%20Node(\u2018b\u2019)%3B%0A%20%0A%20%20%20%20%20%20%20%20list.b%20%3D%20new%20Node(\u2018g\u2019)%3B%0A%20%20%20%20%20%20%20%20list.b.next%20%3D%20new%20Node(\u2018e\u2019)%3B%0A%20%20%20%20%20%20%20%20list.b.next.next%20%3D%20new%20Node(\u2018e\u2019)%3B%0A%20%20%20%20%20%20%20%20list.b.next.next.next%20%3D%20new%20Node(\u2018k\u2019)%3B%0A%20%20%20%20%20%20%20%20list.b.next.next.next.next%20%3D%20new%20Node(\u2018s\u2019)%3B%0A%20%20%20%20%20%20%20%20list.b.next.next.next.next.next%20%3D%20new%20Node(\u2018a\u2019)%3B%0A%20%0A%20%20%20%20%20%20%20%20int%20value%3B%0A%20%20%20%20%20%20%20%20value%20%3D%20list.compare(a%2C%20b)%3B%0A%20%20%20%20%20%20%20%20System.out.println(value)%3B%0A%20%0A%20%20%20%20%7D%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>Java programming-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":31260,"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-27711","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\/27711","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=27711"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27711\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31260"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}