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」
Post a reply