{"id":26936,"date":"2017-12-28T21:43:36","date_gmt":"2017-12-28T16:13:36","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26936"},"modified":"2018-10-29T10:51:20","modified_gmt":"2018-10-29T05:21:20","slug":"java-algorithm-swap-nodes-linked-list-without-swapping-data","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-algorithm-swap-nodes-linked-list-without-swapping-data\/","title":{"rendered":"Swap nodes in a linked list without swapping data"},"content":{"rendered":"<p>Given a <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-linked-list\" target=\"_blank\" rel=\"noopener\">linked list<\/a> and two keys in it, <strong>swap nodes for two given keys<\/strong>. Nodes should be swapped by changing links. <a href=\"https:\/\/www.wikitechy.com\/technology\/java-algorithm-pairwise-swap-elements-given-linked-list\/\" target=\"_blank\" rel=\"noopener\">Swapping<\/a> data of nodes may be expensive in many situations when data contains many fields.<span id=\"more-134695\"><\/span><\/p>\n<p>It may be assumed that all keys in linked list are distinct.<\/p>\n<h3 id=\"examples\"><span style=\"color: #800000;\"><strong>Examples:<\/strong><\/span><\/h3>\n<pre>Input:  10->15->12->13->20->14,  x = 12, y = 20\r\nOutput: 10->15->20->13->12->14\r\n\r\nInput:  10->15->12->13->20->14,  x = 10, y = 20\r\nOutput: 20->15->12->13->10->14\r\n\r\nInput:  10->15->12->13->20->14,  x = 12, y = 13\r\nOutput: 10->15->13->12->20->14<\/pre>\n<p>This may look a simple problem, but is interesting question as it has following cases to be handled.<br \/>\n1) x and y may or may not be adjacent.<br \/>\n2) Either x or y may be a head node.<br \/>\n3) Either x or y may be last node.<br \/>\n4) x and\/or y may not be present in linked list.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>Let us write a clear working code that handles all the above cases:<\/p>\n<p>The idea is to first search x and y in given <a href=\"https:\/\/www.wikitechy.com\/technology\/java-algorithm-find-middle-given-linked-list\/\" target=\"_blank\" rel=\"noopener\">linked list<\/a>. If any of them is not present, then return. While searching for x and y, keep track of <strong>current and previous pointers<\/strong>. First change next of previous pointers, then change next of current pointers. Following is <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-tutorial\" target=\"_blank\" rel=\"noopener\">Java<\/a> implementation of this approach.<\/p>\n<h3 id=\"java-programming-for-swap-nodes-in-a-linked-list\"><span style=\"color: #003366;\"><strong>Java Programming For Swap Nodes in a Linked List:<\/strong><\/span><\/h3>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20swap%20two%20given%20nodes%20of%20a%20linked%20list%0A%20%0Aclass%20Node%0A%7B%0A%20%20%20%20int%20data%3B%0A%20%20%20%20Node%20next%3B%0A%20%20%20%20Node(int%20d)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%2F%2F%20head%20of%20list%0A%20%0A%20%20%20%20%2F*%20Function%20to%20swap%20Nodes%20x%20and%20y%20in%20linked%20list%20by%0A%20%20%20%20%20%20%20changing%20links%20*%2F%0A%20%20%20%20public%20void%20swapNodes(int%20x%2C%20int%20y)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Nothing%20to%20do%20if%20x%20and%20y%20are%20same%0A%20%20%20%20%20%20%20%20if%20(x%20%3D%3D%20y)%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Search%20for%20x%20(keep%20track%20of%20prevX%20and%20CurrX)%0A%20%20%20%20%20%20%20%20Node%20prevX%20%3D%20null%2C%20currX%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(currX%20!%3D%20null%20%26%26%20currX.data%20!%3D%20x)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20prevX%20%3D%20currX%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20currX%20%3D%20currX.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%20Search%20for%20y%20(keep%20track%20of%20prevY%20and%20currY)%0A%20%20%20%20%20%20%20%20Node%20prevY%20%3D%20null%2C%20currY%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(currY%20!%3D%20null%20%26%26%20currY.data%20!%3D%20y)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20prevY%20%3D%20currY%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20currY%20%3D%20currY.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%20either%20x%20or%20y%20is%20not%20present%2C%20nothing%20to%20do%0A%20%20%20%20%20%20%20%20if%20(currX%20%3D%3D%20null%20%7C%7C%20currY%20%3D%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20not%20head%20of%20linked%20list%0A%20%20%20%20%20%20%20%20if%20(prevX%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20prevX.next%20%3D%20currY%3B%0A%20%20%20%20%20%20%20%20else%20%2F%2Fmake%20y%20the%20new%20head%0A%20%20%20%20%20%20%20%20%20%20%20%20head%20%3D%20currY%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20y%20is%20not%20head%20of%20linked%20list%0A%20%20%20%20%20%20%20%20if%20(prevY%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%20%20%20%20prevY.next%20%3D%20currX%3B%0A%20%20%20%20%20%20%20%20else%20%2F%2F%20make%20x%20the%20new%20head%0A%20%20%20%20%20%20%20%20%20%20%20%20head%20%3D%20currX%3B%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20currX.next%3B%0A%20%20%20%20%20%20%20%20currX.next%20%3D%20currY.next%3B%0A%20%20%20%20%20%20%20%20currY.next%20%3D%20temp%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%20Function%20to%20add%20Node%20at%20beginning%20of%20list.%20*%2F%0A%20%20%20%20public%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Node%20new_Node%20%3D%20new%20Node(new_data)%3B%0A%20%0A%20%20%20%20%20%20%20%20new_Node.next%20%3D%20head%3B%0A%20%0A%20%0A%20%20%20%20%20%20%20%20head%20%3D%20new_Node%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20public%20void%20printList()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20tNode%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(tNode%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(tNode.data%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20tNode%20%3D%20tNode.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist%20%3D%20new%20LinkedList()%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F*%20The%20constructed%20linked%20list%20is%3A%0A%20%20%20%20%20%20%20%20%20%20%20%201-%3E2-%3E3-%3E4-%3E5-%3E6-%3E7%20*%2F%0A%20%20%20%20%20%20%20%20llist.push(7)%3B%0A%20%20%20%20%20%20%20%20llist.push(6)%3B%0A%20%20%20%20%20%20%20%20llist.push(5)%3B%0A%20%20%20%20%20%20%20%20llist.push(4)%3B%0A%20%20%20%20%20%20%20%20llist.push(3)%3B%0A%20%20%20%20%20%20%20%20llist.push(2)%3B%0A%20%20%20%20%20%20%20%20llist.push(1)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.print(%22%5Cn%20Linked%20list%20before%20calling%20swapNodes()%20%22)%3B%0A%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%0A%20%20%20%20%20%20%20%20llist.swapNodes(4%2C%203)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.print(%22%5Cn%20Linked%20list%20after%20calling%20swapNodes()%20%22)%3B%0A%20%20%20%20%20%20%20%20llist.printList()%3B%0A%20%20%20%20%7D%0A%7D%0A%2F%2F%20This%20code%20is%20contributed%20by%20Rajat%20Mishra\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output\"><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/h3>\n<pre> Linked list before calling swapNodes() 1 2 3 4 5 6 7\r\n Linked list after calling swapNodes() 1 2 4 3 5 6 7<\/pre>\n<h3 id=\"optimizations\"><span style=\"color: #0000ff;\"><strong>Optimizations:<\/strong> <\/span><\/h3>\n<p>The code can be optimized to search x and y in single traversal. Two <a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/loops-in-java\" target=\"_blank\" rel=\"noopener\">loops<\/a> are used to keep program simple.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Java Algorithm &#8211; Swap nodes in a linked list without swapping data &#8211; Linked List &#8211; Given a linked list and two keys in it, swap nodes for two given keys<\/p>\n","protected":false},"author":1,"featured_media":31615,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[80425,80422,80431,80424,80427,80423,80428,80430,80426,80420,80429,80421],"class_list":["post-26936","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-how-to-swap-two-nodes-in-a-doubly-linked-list-java","tag-swap-adjacent-nodes-in-linked-list","tag-swap-adjacent-nodes-in-linked-list-java","tag-swap-alternate-nodes-linked-list","tag-swap-kth-node-from-beginning-with-kth-node-from-end-in-a-linked-list","tag-swap-nodes-doubly-linked-list","tag-swap-nodes-in-doubly-linked-list-java","tag-swap-nodes-in-linked-list","tag-swap-two-adjacent-elements-linked-list-java","tag-swap-two-nodes-in-a-linked-list-java","tag-swapping-two-nodes-in-a-doubly-linked-list-c","tag-write-a-program-to-reverse-a-linked-list"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26936","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=26936"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26936\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31615"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}