翻转一个链表
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
/**
* Definition for ListNode
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param head: n
* @return: The new head of reversed linked list.
*/
public ListNode reverse(ListNode head) {
// write your code here
ListNode dummy = null;
while (head != null) {
ListNode temp = head.next;
head.next = dummy;
dummy = head;
head = temp;
}
return dummy;
}
}