206. 反转链表

地址:https://leetcode-cn.com/problems/reverse-linked-list/

<?php
/**
反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
 */
/**
 * Definition for a singly-linked list.
 * class ListNode {
 *     public $val = 0;
 *     public $next = null;
 *     function __construct($val) { $this->val = $val; }
 * }
 */
class Solution {

    /**
     * @param ListNode $head
     * @return ListNode
     */
    function reverseList($head) {
        $res = null;
        $current = $head;
        while($current){
            $next = $current->next;
            $current->next = $res;
            $res = $current;
            $current = $next;
        }
        return $res;
    }
}

 

posted @ 2020-09-25 15:49  花花妹子。  阅读(73)  评论(0编辑  收藏  举报