美文网首页
LeetCode 第206题:反转链表

LeetCode 第206题:反转链表

作者: 放开那个BUG | 来源:发表于2020-09-23 21:41 被阅读0次

1、前言

题目描述

2、思路

老题了。

3、代码

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

        ListNode q = head.next;
        ListNode p = q;
        head.next = null;
        while(q != null){
            p = q.next;
            q.next = head;
            head = q;
            q = p;
        }

        return head;
    }
}

相关文章

网友评论

      本文标题:LeetCode 第206题:反转链表

      本文链接:https://www.haomeiwen.com/subject/nxbkrktx.html