反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
迭代:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
ListNode next = null; // 下一个节点
while (cur != null) {
// 拿到原来链表head的下一个节点
next = cur.next;
// 把当前链表的下一个节点指向上一个节点也就是pre
cur.next = pre;
// 重置pre为当前链表节点
pre = cur;
// 重置当前节点
cur = next;
}
// 返回反转后的链表 也就是pre 其实就是cur
return pre;
}
}
或者
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode newHead=null;
while(head!=null){
ListNode next=head.next;
head.next=newHead;
newHead=head;
head=next;
}
return newHead;
}
}
递归:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null)return head;
ListNode rev=reverseList(head.next);
head.next.next=head;
head.next=null;
return rev;
}
}