15反转链表

2019-08-12  本文已影响0人  Bing_o_o

题目描述

输入一个链表,反转链表后,输出新链表的表头。

Java实现

class ListNode {
    int val;
    ListNode next;

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

public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode prev = null, curr = head;
        while (curr != null) {
            ListNode temp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = temp;
        }
        return prev;
    }
}
上一篇下一篇

猜你喜欢

热点阅读