链表(一)——翻转链表

2018-09-24  本文已影响0人  旺叔叔

LeetCode_209_ReverseLinkedList

题目解析:

注意这里是没有头结点的链表,每个节点都有值。

解法:

public static ListNode reverseList(ListNode head) {
    if(null == head)
        return head;
    ListNode preNode = null;
    ListNode nextNode = null;
    while(null != head.next){
        nextNode = head.next;
        head.next = preNode;
        preNode = head;
        head = nextNode;
    }
    head.next = preNode;
    return head;
}
上一篇 下一篇

猜你喜欢

热点阅读