分别实现反转单向和双向链表的函数
看代码:
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; } }
时间: 2024-10-18 11:48:43