首页 > 代码库 > leetcode-206

leetcode-206

Reverse Linked List

Reverse a singly linked list.

此题不难,附上java代码:

/**
 * 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 pre=head;
        ListNode p=head.next;
        pre.next=null;
        ListNode nxt;
        while(p!=null){
            nxt=p.next;
            p.next=pre;
            pre=p;
            p=nxt;
        }
        return pre;
    }
}

  

 

leetcode-206