Thursday, November 5, 2015

[leetcode] reverse Linked List

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) return head;
        ListNode previous = head;
        ListNode current = head.next;
        previous.next = null;
        ListNode next;
        while (current != null){
            next = current.next;
            current.next = previous;
            previous = current;
            current = next;
        }
        return previous;
    }
}

No comments:

Post a Comment