LeetCode – 206. Reverse Linked List

Reverse a singly linked list.

这道题非常基础,需要熟练掌握

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

This site Original article All followed" Attribution—NonCommercial—ShareAlike 4.0 (CC BY-NC-SA 4.0) ”。 Please keep the following marks for sharing and interpretation:

Original author: Jake Tao Source: 「LeetCode – 206. Reverse Linked List」

Praise 198
0 0 198

Further reading

Post a reply

Log in can only be commented on later
Share this page
Back to top