From f6528c453ce7bbf1935b169ff27af195a65feb15 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 7 Jun 2021 13:17:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=91=E6=8C=87=20Offer=2024:=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/FanZhuanLianBiaoLcof.java | 61 +++++++++++++++++++ .../editor/cn/FanZhuanLianBiaoLcof.md | 19 ++++++ 2 files changed, 80 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/FanZhuanLianBiaoLcof.java create mode 100644 src/main/java/leetcode/editor/cn/FanZhuanLianBiaoLcof.md diff --git a/src/main/java/leetcode/editor/cn/FanZhuanLianBiaoLcof.java b/src/main/java/leetcode/editor/cn/FanZhuanLianBiaoLcof.java new file mode 100644 index 0000000..c8e030b --- /dev/null +++ b/src/main/java/leetcode/editor/cn/FanZhuanLianBiaoLcof.java @@ -0,0 +1,61 @@ +//定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。 +// +// +// +// 示例: +// +// 输入: 1->2->3->4->5->NULL +//输出: 5->4->3->2->1->NULL +// +// +// +// 限制: +// +// 0 <= 节点个数 <= 5000 +// +// +// +// 注意:本题与主站 206 题相同:https://leetcode-cn.com/problems/reverse-linked-list/ +// Related Topics 链表 +// 👍 243 👎 0 + +package leetcode.editor.cn; + +import com.code.leet.entiy.ListNode; + +//剑指 Offer 24:反转链表 +public class FanZhuanLianBiaoLcof{ + public static void main(String[] args) { + //测试代码 + Solution solution = new FanZhuanLianBiaoLcof().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode reverseList(ListNode head) { + ListNode newHead = null; + ListNode p = head; + ListNode pPrev = null; + while (p != null) { + ListNode temp = p.next; + if (temp == null) { + newHead = p; + } + p.next = pPrev; + pPrev = p; + p = temp; + } + return newHead; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/FanZhuanLianBiaoLcof.md b/src/main/java/leetcode/editor/cn/FanZhuanLianBiaoLcof.md new file mode 100644 index 0000000..3b83a53 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/FanZhuanLianBiaoLcof.md @@ -0,0 +1,19 @@ +

定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

+ +

 

+ +

示例:

+ +
输入: 1->2->3->4->5->NULL
+输出: 5->4->3->2->1->NULL
+ +

 

+ +

限制:

+ +

0 <= 节点个数 <= 5000

+ +

 

+ +

注意:本题与主站 206 题相同:https://leetcode-cn.com/problems/reverse-linked-list/

+
Related Topics
  • 链表
  • \n
  • 👍 243
  • 👎 0
  • \ No newline at end of file