diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java index 104b9598022c..fb33e6e6ecf7 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java @@ -56,6 +56,54 @@ public boolean detectLoop() { return flag; } + /** + * Swaps nodes of two given values a and b. + * + */ + public void swapNodes(int valueFirst, int valueSecond) { + if(valueFirst == valueSecond){ + return; + } + Node previousA = null ,currentA = head; + while(currentA != null && currentA.value != valueFirst){ + previousA = currentA; + currentA = currentA.next; + } + + Node previousB = null ,currentB = head; + while(currentB != null && currentB.value != valueSecond){ + previousB = currentB; + currentB = currentB.next; + } + /** If either of 'a' or 'b' is not present, then return */ + if(currentA == null || currentB == null){ + return; + } + + // If 'a' is not head node of list + if(previousA != null){ + previousA.next = currentB; + } + else{ + // make 'b' as the new head + head = currentB; + } + + // If 'b' is not head node of list + if(previousB != null){ + previousB.next = currentA; + } + else{ + // Make 'a' as new head + head = currentA; + } + // Swap next pointer + + Node temp = currentA.next; + currentA.next = currentB.next; + currentB.next = temp; + } + /** * Reverse a singly linked list from a given node till the end * diff --git a/src/main/java/com/thealgorithms/datastructures/stacks/DuplicateBrackets.java b/src/main/java/com/thealgorithms/datastructures/stacks/DuplicateBrackets.java index 1195dea36622..4b86cb499aa3 100644 --- a/src/main/java/com/thealgorithms/datastructures/stacks/DuplicateBrackets.java +++ b/src/main/java/com/thealgorithms/datastructures/stacks/DuplicateBrackets.java @@ -38,6 +38,7 @@ public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.println(check(str)); + sc.close(); } }