leetcode:【集合】Reverse Linked List

| 分类 算法  | 标签 leetcode  collection 

问题描述

翻转
Reverse Linked List  

code

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null) return null;
        ListNode q = head;
        ListNode p = head.next;
        while(p != null) {
            ListNode temp = p.next;
            p.next = q;
            q = p;
            p = temp;
        }
        head.next = null;
        head = q;
        return head;
    }
}

上一篇     下一篇