# 69. 反转链表

java

```java
import leetcode.common.LinkedUtils;
import leetcode.common.ListNode;


/**
 * 反转链表
 *
 * 输入一个链表，反转链表后，输出新链表的表头。
 */
public class ReverseListSolution {
    public ListNode ReverseList(ListNode head) {
        if(head==null) return null;
        //pr:指向要反转的下一个元素
        //p:指向当前正在反转的元素
        //q:指向上一个反转的元素
        ListNode pr=head,p=head,q=null;
        while (pr!=null){
            pr=p.next;
            p.next=q;
            q=p;
            p=pr;
        }

        head=q;
        return head;
    }

    public static void main(String[] args){
        int[] array={
                1,2,3,4,5,6
        };
        ListNode head=LinkedUtils.arrayToLinkedList(array);
        ListNode r=new ReverseListSolution().ReverseList(head);
        LinkedUtils.print(r);
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/69-fan-zhuan-lian-biao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
