首页 > 编程语言 > 详细

算法复习之 链表反转

时间:2021-04-28 22:12:18      阅读:29      评论:0      收藏:0      [点我收藏+]

思路:链表反转,我这里采用的是JDK 1.7中HashMap拉链法所采用的头插法。

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/

import java.util.*;

public class Solution {
    public ListNode ReverseList(ListNode head) {
        /*头插法*/
        ListNode next = null;
        ListNode pre = null;
        while(head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}

算法复习之 链表反转

原文:https://www.cnblogs.com/bianjunting/p/14715742.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!