剑指offer---反转链表

China☆狼群 提交于 2020-03-03 21:09:12

题目描述:输入一个链表,反转链表后,输出新链表的表头。
思路:定义两个指针,反向输出

package Function;
//迭代:两个指针,反向输出,时间复杂度:O(n),空间复杂度:O(1)
import Test.ListNode;
public class reverseList24 {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode tmp = curr.next;
            curr.next = pre;
            pre = curr;
            curr = tmp;
        } return pre;
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!