算法总结之 反转单向和双向链表

分别实现反转单向和双向链表的函数

看代码:

package TT;

public class Test88 {

    public class Node{
         public int value;
         public Node next;
         public Node(int data){
              this.value = data;
         }
    }
    
    public Node reverseList(Node head){
          Node pre = null;
          Node next = null;
          while(head!=null){
              next = head.next;
              head.next = pre;
              pre = head;
              head = next;
          }
          return pre;
    }

}

 可以用栈实现:

  

class Node {
    int value;
    Node next;

    public Node(int value) {
        this.value = value;
    }
}
public class Test19 {

    public static Node getReverseList(Node node) {
        Stack<Node> stack = new Stack<>();
        while (node != null) {
            Node now = node;
            stack.push(now);
            node=node.next;
        }
       Node pre = stack.pop();
       Node result = pre;
       Node next = null;
        while (!stack.isEmpty()) {
            next = stack.pop();
            pre.next=next;
            pre=next;
        }
        next.next=null;
        return result;
    }
    public static void main(String[] args) {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);

        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=null;

        Node resultNode = getReverseList(node1);
        while (resultNode !=null){
            System.out.println(resultNode.value);
            resultNode=resultNode.next;
        }
    } 

}

 

反向双向链表

 

package TT;



import TT.Test86.DoubleNode;

public class Test89 {

    public DoubleNode{
        public int  value;
        public DoubleNode last;
        public DoubleNode pre;
        public DoubleNode(int data){
            this.value = data;
        }
    }
    
    public DoubleNode reverseList(DoubleNode head){
        DoubleNode pre = null;
        DoubleNode next = null;
        while(head!=null){
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
            
            
        }
        return pre;
    }
        
}

 调节指针的算法:

class Node {
    int value;
    Node next;

    public Node(int value) {
        this.value = value;
    }
}
public class Test19 {

    public static Node getReverseList(Node node) {
        Node last = null;
        Node pre = node;
        Node next = node.next;
        while (pre.next != null){
           pre.next=last;
           last=pre;
           pre= next ;
           next=next.next;
        }
        pre.next=last;
        return  pre;
    }
    public static void main(String[] args) {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);

        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=null;

        Node resultNode = getReverseList(node1);
        while (resultNode !=null){
            System.out.println(resultNode.value);
            resultNode=resultNode.next;
        }
    } 

}

 

posted @ 2017-09-09 20:26  toov5  阅读(235)  评论(0编辑  收藏  举报