Iteration:

代码:

/**
* 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) return null;
ListNode tail = head.next;
ListNode cur = head;
while(tail!= null){
ListNode nex = tail.next;
tail.next = head;
cur.next = nex;
head = tail;
tail = nex;
}
return head;
}
}

 

Recursion:

代码:

/**
* 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 cur = reverseList(head.next);
head.next.next = head;
head.next = null;
return cur;

}

}

posted on 2016-01-11 13:17  爱推理的骑士  阅读(114)  评论(0编辑  收藏  举报