链表反转

        题目一:反转单链表

        核心代码:

/**
	 * 反转单链表
	 * @param head
	 * @return
	 */
	public Node reverseList(Node head){
		Node pre = null; // 指向当前节点的前一个节点的指针
		Node cur = head; // 指向当前节点的指针
		Node next = null; // 指向当前节点的下一个节点的指针
		while(cur != null){
			next = cur.next; // 把当前节点的下一个节点保存起来
			cur.next = pre; // 将当前节点的next域指向前一个节点
			pre = cur; // 将指向前一个节点的指针指向当前节点
			cur = next; // 将指向当前节点的指针指向下一个节点
		}
		return pre;
	}

        完整代码:

package cn.ccn.reverse;

// 链表节点的定义
class Node{
	public int value;
	public Node next;
	public Node(int value) {
		super();
		this.value = value;
	}
}

public class Reverse {
	public void printList(Node head){
		System.out.print("print list: ");
		Node cur = head;
		while(cur != null){
			System.out.print(cur.value + " ");
			cur = cur.next;
		}
		System.out.println();
	}
	
	/**
	 * 反转单链表
	 * @param head
	 * @return
	 */
	public Node reverseList(Node head){
		Node pre = null; // 指向当前节点的前一个节点的指针
		Node cur = head; // 指向当前节点的指针
		Node next = null; // 指向当前节点的下一个节点的指针
		while(cur != null){
			next = cur.next; // 把当前节点的下一个节点保存起来
			cur.next = pre; // 将当前节点的next域指向前一个节点
			pre = cur; // 将指向前一个节点的指针指向当前节点
			cur = next; // 将指向当前节点的指针指向下一个节点
		}
		return pre;
	}
	
	public static void main(String[] args) {
		Node n1 = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		Node n5 = new Node(5);
		n1.next = n2;
		n2.next = n3;
		n3.next = n4;
		n4.next = n5;
		Reverse revserse = new Reverse();
		revserse.printList(revserse.reverseList(n1));
	}
}

        题目二:反转双链表

        核心代码:

/**
	 * 反转双链表
	 * @param head
	 * @return
	 */
	public DoubleNode reverse(DoubleNode head){
		DoubleNode pre = null;
		DoubleNode cur = head;
		DoubleNode next = null;
		while(cur != null){
			next = cur.right; // next指向当前节点的下一个节点
			cur.left = pre; // 当前节点的左节点指向前一个节点
			cur.right = next; // 当前节点的右节点指向下一个节点
			pre = cur; // 让pre指向当前节点
			cur = next; // cur指向next
		}
		return pre;
	}

         完整代码:

package cn.ccn.reverse;

// 双链表节点的定义
class DoubleNode{
	public int value;
	public DoubleNode left;
	public DoubleNode right;
	public DoubleNode(int value) {
		this.value = value;
	}
}

public class ReverseTwo {
	public void printDoubleList(DoubleNode head){
		System.out.print("print double list:");
		DoubleNode cur = head;
		while(cur != null){
			System.out.print(cur.value + " ");
			cur = cur.right;
		}
		System.out.println();
	}
	
	public void printReverseDoubleList(DoubleNode head){
		System.out.print("print double list:");
		DoubleNode cur = head;
		while(cur != null){
			System.out.print(cur.value + " ");
			cur = cur.left;
		}
		System.out.println();
	}
	
	/**
	 * 反转双链表
	 * @param head
	 * @return
	 */
	public DoubleNode reverse(DoubleNode head){
		DoubleNode pre = null;
		DoubleNode cur = head;
		DoubleNode next = null;
		while(cur != null){
			next = cur.right; // next指向当前节点的下一个节点
			cur.left = pre; // 当前节点的左节点指向前一个节点
			cur.right = next; // 当前节点的右节点指向下一个节点
			pre = cur; // 让pre指向当前节点
			cur = next; // cur指向next
		}
		return pre;
	}
	
	public static void main(String[] args) {
		DoubleNode n1 = new DoubleNode(1);
		DoubleNode n2 = new DoubleNode(2);
		DoubleNode n3 = new DoubleNode(3);
		DoubleNode n4 = new DoubleNode(4);
		DoubleNode n5 = new DoubleNode(5);
		n1.right = n2;
		n2.left = n1;
		n2.right = n3;
		
		n3.left = n2;
		n3.right = n4;
		
		n4.left = n3;
		n4.right = n5;
		
		n5.left = n4;
		
		ReverseTwo reverseTwo = new ReverseTwo();
		reverseTwo.printDoubleList(n1);
		reverseTwo.printReverseDoubleList(reverseTwo.reverse(n1));
	}
}

        题目三:反转部分链表

        核心代码:

public ListNode reversePart(ListNode head, int from ,int to){
		int length = 0; // 记录链表的长度
		ListNode cur = head; // 用来表示当前节点
		ListNode pre = null; // 用来记录当前节点的前一个节点
		ListNode next = null; // 用来表示当前节点的下一个节点
		ListNode fPre = null; // form的前一个节点
		ListNode tNext = null; // to的后一个节点
		while(cur != null){
			length++;
			fPre = length==(from-1)?cur:fPre;
			tNext = length==(to+1)?cur:tNext;
			cur = cur.next;
		}
		if(from>to || from<1 || to>length){
			return head;
		}
		pre = fPre==null?head:fPre.next;
		cur = pre.next;
		pre.next = tNext;
		while(cur != tNext){
			next = cur.next;
			cur.next = pre;
			pre = cur;
			cur = next;
		}
		if(fPre != null){
			fPre.next = pre;
			return head;
		}
		return pre;
	}

        完整代码:

package cn.ccn.reverse;

class ListNode{
	public int value;
	public ListNode next;
	public ListNode(int value) {
		super();
		this.value = value;
	}
}

public class ReverseThree {
	// 打印单链表
	public void printList(ListNode head){
		System.out.print("print list :");
		ListNode cur = head;
		while(cur != null){
			System.out.print(cur.value + " ");
			cur = cur.next;
		}
		System.out.println();
	}
	
	public ListNode reversePart(ListNode head, int from ,int to){
		int length = 0; // 记录链表的长度
		ListNode cur = head; // 用来表示当前节点
		ListNode pre = null; // 用来记录当前节点的前一个节点
		ListNode next = null; // 用来表示当前节点的下一个节点
		ListNode fPre = null; // form的前一个节点
		ListNode tNext = null; // to的后一个节点
		while(cur != null){
			length++;
			fPre = length==(from-1)?cur:fPre;
			tNext = length==(to+1)?cur:tNext;
			cur = cur.next;
		}
		if(from>to || from<1 || to>length){
			return head;
		}
		pre = fPre==null?head:fPre.next;
		cur = pre.next;
		pre.next = tNext;
		while(cur != tNext){
			next = cur.next;
			cur.next = pre;
			pre = cur;
			cur = next;
		}
		if(fPre != null){
			fPre.next = pre;
			return head;
		}
		return pre;
	}
	
	public static void main(String[] args) {
		ListNode n1 = new ListNode(1);
		ListNode n2 = new ListNode(2);
		ListNode n3 = new ListNode(3);
		ListNode n4 = new ListNode(4);
		ListNode n5 = new ListNode(5);
		
		n1.next = n2;
		n2.next = n3;
		n3.next = n4;
		n4.next = n5;
		ReverseThree reverseThree = new ReverseThree();
		reverseThree.printList(n1);
		reverseThree.printList(reverseThree.reversePart(n1, 5, 5));
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值