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;
    }
}

  

 

posted @ 2017-03-12 12:42  世人谓我恋长安  阅读(100)  评论(0编辑  收藏  举报