leetcode刷题----反转链表

val = $val;
 *         $this->next = $next;
 *     }
 * }
 */
class Solution {

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

你可能感兴趣的:(leetcode,leetcode,链表,算法)