Java-Leetcode刷题模板
主要是要把所有的写在一个文件里,然后想象成和python那个刷题板子差不多的流程
以翻转链表这个题为例:https://leetcode.cn/problems/reverse-linked-list/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package cn.leetcode.codetop.jan05;
class ListNode { int val; ListNode next;
ListNode() {
}
ListNode(int val, ListNode next) { this.val = val; this.next = next; } }
class Solution206 { public ListNode solution(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } }
public class Main206ReverseList { public static void main(String[] args) { ListNode b = new ListNode(3, null); ListNode a = new ListNode(2, b); ListNode head = new ListNode(1, a);
Solution206 S = new Solution206(); ListNode res = S.solution(head);
while (res != null) { System.out.println(res.val); res = res.next; } } }
|