单向链表双向链表翻转练习

public static class Node {
		public int value;
		public Node next;

		public Node(int data) {
			value = data;
		}
	}

	public static class DoubleNode {
		public int value;
		public DoubleNode last;
		public DoubleNode next;

		public DoubleNode(int data) {
			value = data;
		}
	}

	//  head
	//   a    ->   b    ->  c  ->  null
	//   c    ->   b    ->  a  ->  null
	public static Node reverseLinkedList(Node head) {
		Node pre = null;
		Node next = null;
		while (head != null) {
			//拿到下一个
			next = head.next;
			//将head 指向上一个
			head.next = pre;
			//将pre上一个变成当前head
			pre = head;
			//继续转换下一个
			head = next;
		}
		return pre;
	}
	
	 
	public static DoubleNode reverseDoubleList(DoubleNode head) {
		//上一个
		DoubleNode pre = null;
		//下一个
		DoubleNode next = null;
		while (head != null) {
			//拿到下一个
			next = head.next;
			//将head 下一个 指向上一个
			head.next = pre;
			//将head 上一个 指向下一个
			head.last = next;
			//将pre上一个变成当前head
			pre = head;
			//将 当前head 变成下一个 下一次循环就从下一个开始了
			head = next;
		}
		return pre;
	}

你可能感兴趣的:(算法,链表,数据结构)