Java-链表的部分翻转

package 链表_递归_栈;
import java.util.Random;
/*
 * 问题:给定一个链表,翻转该链表从m到n的位置。要求:直接翻转而非申请新空间
 * 以第四个和第八个结点为范围:翻转前的链表:13,96,31,66,71,16,43,78,71,1,翻转后的链表:13,96,31,78,43,16,71,66,71,1,
 * */
public class 链表的部分翻转 {

	public static void main(String[] args) throws Exception {
		ListNode h1 = getList(10);
		
		System.out.print("翻转前的链表:");
		ListNode l1 = h1.nextNode;
		while(l1 != null){
			System.out.print(l1.value + ",");
			l1 = l1.nextNode;
		}
		System.out.println();
		
		System.out.print("翻转后的链表:");	
		ListNode headList = reverse(h1, 4, 8);
		ListNode l2 = headList.nextNode;
		while(l2 != null){
			System.out.print(l2.value + ",");
			l2 = l2.nextNode;
		}
	}
	
	//翻转链表
	public static ListNode reverse(ListNode head, int m, int n) throws Exception{
		if(m > n){
			throw new  Exception("输入的翻转的范围有误!");
		}
		ListNode Head = head;
		ListNode cur = head.nextNode;
		int i;
		for(i = 0; i < m - 1; i++){//空循环找到m-1处
			//System.out.println(cur.value + ".");
			head = cur;
			cur =  cur.nextNode;
		}
		
		ListNode pre = cur;//pre指向第m-1个结点的位置
		cur = cur.nextNode ;//第m个结点
		ListNode pnext;
		for(; i < n - 1; i++){//结合链表图来分析
			pnext = cur.nextNode;
			cur.nextNode = head.nextNode;
			head.nextNode = cur;
			pre.nextNode = pnext;
			cur = pnext;
		}
		return Head;
	}
	
	//随机生成一个链表,其中参数length是链表结点的个数
	public static ListNode getList(int length){
		Random r = new Random();
		if(length < 0){
			return null;
		}
		ListNode head = new ListNode();
		ListNode point = head;
		if(length == 0){
			return head;
		}
		
		for(int i = 0; i < length; i++){
			ListNode p = new ListNode(r.nextInt(100));
			point.nextNode = p;
			point = point.nextNode;
			//System.out.println(point.value + ",");
		}
		return head;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值