美文网首页ACM题库~
LeetCode 206. Reverse Linked Lis

LeetCode 206. Reverse Linked Lis

作者: 关玮琳linSir | 来源:发表于2017-10-23 19:55 被阅读14次

Reverse a singly linked list.

click to show more hints.

Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?

题意:逆序一个链表

java代码:

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

相关文章

网友评论

    本文标题:LeetCode 206. Reverse Linked Lis

    本文链接:https://www.haomeiwen.com/subject/jntzuxtx.html