206. Reverse Linked List

Total Accepted: 81448
Total Submissions: 214931
Difficulty: Easy

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大部分是最快的,难道是easy题目的原因?

Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 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) {
        ListNode re=null,t;
        while(head!=null){
            t=head.next;
            head.next=re;
            re=head;
            head=t;
        }
        return re;
    }
}

打个小广告

欢迎加入我的小专栏「基你太美」一起学习。