206. Reverse Linked List

来源:互联网 发布:淘宝分享代码下载 编辑:程序博客网 时间:2024/05/21 10:54

Reverse a singly linked list.

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */class Solution {    public ListNode reverseList(ListNode head) {        ListNode pre = null;ListNode cur = head;while (cur != null) {ListNode next = cur.next;cur.next = pre; // 将当前节点的next指针指向前一个节点pre = cur; // 将pre后移cur = next; // 将cur后移}return pre;    }}

原创粉丝点击