剑指offer第二版-24反转链表

/**
 * 反转链表
 * <p>
 * 想要链表反转时不断裂,至少需要3个变量记录,pre,cur,post。
 * 与前面的题目类似,初始化pre为null,cur为head,post为head.next。
 * 初始化之前要注意检查链表的长度。
 *
 * @author VicterTian
 * @version V1.0
 * @Date 2019/2/25
 */
public class P142_ReverseList {
	static class ListNode<T> {
		T val;
		ListNode<T> next;

		ListNode(T val) {
			this.next = null;
			this.val = val;
		}

		@SuppressWarnings("all")
		@Override
		public String toString() {
			StringBuilder ret = new StringBuilder();
			ret.append("[");
			for (ListNode cur = this; ; cur = cur.next) {
				if (cur == null) {
					ret.deleteCharAt(ret.lastIndexOf(" "));
					ret.deleteCharAt(ret.lastIndexOf(","));
					break;
				}
				ret.append(cur.val);
				ret.append(", ");
			}
			ret.append("]");
			return ret.toString();
		}
	}

	private static ListNode reverseList(ListNode head) {
		if (head == null ) {
			return null;
		}
		// 说明两边搜长度为一,可以直接返回它本身
		if (head.next == null){
			return head;
		}
		ListNode pre = null;
		ListNode cur = head;
		ListNode post = head.next;
		while (true) {
			cur.next = pre;
			pre = cur;
			cur = post;
			if (post != null) {
				post = post.next;
			} else {
				return pre;
			}
		}
	}
	public static void main(String[] args){
		ListNode<Integer> head = new ListNode<>(1);
/*		head.next= new ListNode<>(2);
		head.next.next = new ListNode<>(3);*/
		System.out.println(head);
		head = reverseList(head);
		System.out.println(head);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值