206. Reverse Linked List

来源:互联网 发布:2017淘宝客推广技巧 编辑:程序博客网 时间:2024/06/05 06:19

Reverse a singly linked list.

/** * 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||head.next==null) return head;        ListNode A= head, B=A.next, C = B.next;        while(B!=null){            C=B.next;            B.next=A;            A=B;            B=C;        }        head.next = null;        return A;    }}


0 0