本文共 579 字,大约阅读时间需要 1 分钟。
输入一个链表,反转链表后,输出新链表的表头。
{1,2,3}
{3,2,1}
说明:本题目包含复杂数据结构ListNode
/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode ReverseList(ListNode head) { if(head == null) return null; ListNode pre = null; ListNode next = null; while(head != null) { next = head.next; //断开下结点与当前结点的连接, 与前一结点连接 head.next = pre; pre = head; head = next; } return pre; }}
转载地址:http://snpgz.baihongyu.com/