Reverse Linked List

Reverse Linked List

问题:

Reverse a singly linked list.

思路:

  插入排序的应用

我的代码:

public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null || head.next==null)   return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode cur = head.next;
        while(cur != null)
        {
            ListNode next = cur.next;
            cur.next = dummy.next;
            dummy.next = cur;
            cur = next;
        }
        head.next = null;
        return dummy.next;
    }
}
View Code

 

posted on 2015-05-05 14:44  zhouzhou0615  阅读(105)  评论(0编辑  收藏  举报

导航