1.单(双)链表反转

发布时间 2023-07-09 16:23:43作者: 郑择徽

public static class Node{
    public int value;
    public Node next;
    public Node(int data){
        this.value=data;    
    }
}

 

public static Node reverseLinkedList(Node head){
     Node pre=null;
     Node next=null;
     while(head!=null){
         next=head.next;
         head.next=pre;
         pre=head;
         head=next;
     } 
     return pre;
}



public static DoubleNode reverseDoubleList(Node 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;
}