逆置链表中的一段

package com.zuochengyun.book.chaptertwo.list;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 反转一个链表的一段       
 * @author 
 *
 */
public class ListRevertNToN {
	
	private static Node doRevertListNFromN(Node head,int from, int to) {
		if(head==null || from < 0 || to < from) {
			return null;
		}
		if(to==from) {
			return head;
		}
		//新建一个head 方便操作
		Node myHead=new Node(0);
		myHead.next=head;
		Node currentNode=myHead;
		int index=0;
		Node firstEnd=null;
		Node secondHead=null;
		//要逆置list其中的一段,可以把链表看成三段
		while(currentNode !=null) {
			//记录第一段最后一个结点
			if(index==from-1) {
				firstEnd=currentNode;
			}
			//获取最后一段的开头
			if(index == to+1) {
				secondHead=currentNode;
				break;
			}
			index++;
			currentNode=currentNode.next;
		}
		if(to >= index) {
			return null;
		}
		//第一段和第二段开头连接好
		firstEnd.next=doPartRevert(firstEnd.next, secondHead);
		return myHead.next;
	}
	/*
	 * 逆置第二段链表,并且和第三段链表连接好
	 */
	private static Node doPartRevert(Node begin,Node end) {
		Node pre=end;
		Node next=begin;
		while(next!=null && next!=end) {
			next=begin.next;
			begin.next=pre;
			pre=begin;
			begin=next;
		}
		return pre;
	}
	private static Node doRevert(Node head) {
		Node pre = null;
		Node next = head;
		while(next != null) {
			next = head.next;
			head.next= pre;
			pre = head;
			head=next;
		}
		return pre;
	}
	public static void main(String[] args) {
		Node head=doRevertListNFromN(MyListUtils.cteateSigleList(), 2, 3);
		//输出链表
		MyListUtils.printList(head);
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值