1. 程式人生 > >算法總結之 反轉單向和雙向鏈表

算法總結之 反轉單向和雙向鏈表

while turn double logs pub pan != package port

分別實現反轉單向和雙向鏈表的函數

看代碼:

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; } }

反向雙向鏈表

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; } }

算法總結之 反轉單向和雙向鏈表