Friday, April 21, 2017

Reverse Linked List

Reverse Linked List

Reverse a singly linked list.


Thoughts:
Need to familiar with Pointers in LinkedList.

Java Solutions:
public class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        while(head != null){
            ListNode temp = head.next;
            head.next = pre;
            pre = head;
            head = temp;
        }
        return pre;
    }
}




No comments:

Post a Comment