Files
leetcode/206.reverse-linked-list.java
2025-10-24 11:28:01 +08:00

32 lines
651 B
Java

/*
* @lc app=leetcode id=206 lang=java
*
* [206] Reverse Linked List
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null, cur = head, t;
while(cur != null) {
t = cur.next;
cur.next = prev;
prev = cur;
cur = t;
}
return prev;
}
}
// @lc code=end