2
\$\begingroup\$

Are there any corner cases missing here?

class Node
{
    int data;
    Node next;
    Node prev;
}

Node reverseDLL(Node head) 
{
    if(head == null || head.next == null)
       return head;

    Node previousNode = head.prev;
    Node currentNode = head;
    Node nextNode; 

    while(currentNode!=null)
    {
       nextNode = currentNode.next;

       currentNode.next = previousNode;
       currentNode.prev = nextNode;

       previousNode = currentNode;
       currentNode = nextNode;
    }

    head = previousNode;
    return  head;
}
\$\endgroup\$

closed as off-topic by dfhwze, pacmaninbw, t3chb0t, Mast, VisualMelon Sep 1 at 9:06

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – dfhwze, pacmaninbw, t3chb0t, VisualMelon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    \$\begingroup\$ Write unit tests and find out. \$\endgroup\$ – John Deters Oct 4 '16 at 3:16
  • 2
    \$\begingroup\$ There is a (slightly) simpler approach. Each node in your doubly linked list has references to the previous and next nodes, so all you really have to do is travel to each node in the list and swap them. The only tricky part is that after the swap, you need to use the node's prev to get to what's (in your view) the next node. \$\endgroup\$ – Jerry Coffin Oct 4 '16 at 4:07
  • \$\begingroup\$ This question lacks context how the nodes are linked. Is the list circular or not? \$\endgroup\$ – dfhwze Aug 31 at 10:20
  • \$\begingroup\$ For us to be able to review this it would help to see the add node, insert node, delete node and the forward traverse methods as well as the reverse method. \$\endgroup\$ – pacmaninbw Aug 31 at 17:02
  • \$\begingroup\$ This is probably a doubly-linked-list, but we need more information about your specification to be of proper help. \$\endgroup\$ – Mast Aug 31 at 20:22
1
\$\begingroup\$

If we are allowed to pass the tail node to the reversal method (Node reverseDLL(Node head, Node tail)), we can perform the reversal by reversing the data integer fields instead of restructuring the list. All in all, I had this in mind:

import java.util.ArrayList;
import java.util.List;

class Node {
    int data;
    Node next;
    Node prev;

    Node(int data) {
        this.data = data;
    }
}

public class ReverseDLL {

    static void reverseDLL2(Node head, Node tail) {
        Node left = head; 
        Node right = tail;

        while (true) {
            if (left == right) {
                return;
            }

            int tmp = left.data;
            left.data = right.data;
            right.data = tmp;

            if (left.next == right) {
                return;
            }

            left = left.next;
            right = right.prev;
        }
    }

    static void printList(Node head) {
        for (Node current = head; current != null; current = current.next) {
            System.out.print(current.data);
            System.out.print(" ");
        }
    }

    public static void main(String[] args) {
        List<Node> nodes = new ArrayList<>();

        for (int i = 0; i < 10; ++i) {
            nodes.add(new Node(i));
        }

        for (int i = 0; i < 9; ++i) {
            nodes.get(i).next = nodes.get(i + 1);
        }

        for (int i = 1; i < 10; ++i) {
            nodes.get(i).prev = nodes.get(i - 1);
        }

        printList(nodes.get(0));
        reverseDLL2(nodes.get(0), nodes.get(9));
        System.out.println();
        printList(nodes.get(0));
        reverseDLL2(null, null);
    }
}

Hope that helps.

\$\endgroup\$
  • \$\begingroup\$ This works fine if you have simple data types as the payload, but if you have more complex objects in there, depending on the case you may need to build a copy function, maybe end up using more memory and of course more code = higher chance of bugs. Also, it would be nice to check for circular lists. \$\endgroup\$ – ChatterOne Feb 1 '17 at 10:02
  • \$\begingroup\$ @ChatterOne Not true. In Java, we deal with references to objects, and not objects themselves, so the reversal operation would be the same since we simply swap references. \$\endgroup\$ – coderodde Feb 1 '17 at 10:35
  • 1
    \$\begingroup\$ You're right, I'm digging too much into Python these days, I guess \$\endgroup\$ – ChatterOne Feb 1 '17 at 12:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.