diff --git a/README.md b/README.md index ec4c4d4..f0ae161 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -# 算法模板(Java版) +# 算法模板(Python版-杀穿!) > 本项目内容主要参考[算法模板](https://github.com/greyireland/algorithm-pattern),在此感谢原作者的整理与贡献 在原项目的基础上,尽量保持了原有的结构划分,并做了一点微小的工作: -- 代码改写为`java`语言版本 +- 代码改写为`python`语言版本 - 调整内容,新增了几种算法 - 补充部分[leetcode中国站](https://leetcode-cn.com/)的题目链接 - 书籍链接:[GitBook](https://chienmy.gitbook.io/algorithm-pattern-java/) diff --git a/basic_algorithm/dp.md b/basic_algorithm/dp.md index e3383a5..b25cb8b 100644 --- a/basic_algorithm/dp.md +++ b/basic_algorithm/dp.md @@ -350,7 +350,7 @@ public int lengthOfLIS(int[] nums) { } ``` -#### 单词拆分 +#### 单词拆分(目前还不太熟悉,重点重复刷) > [139. 单词拆分](https://leetcode-cn.com/problems/word-break/) > diff --git a/data_structure/linked_list.md b/data_structure/linked_list.md index 3e9e833..ce2d8e5 100644 --- a/data_structure/linked_list.md +++ b/data_structure/linked_list.md @@ -83,22 +83,35 @@ public ListNode deleteDuplicates(ListNode head) { > 反转一个单链表。 思路:用一个 prev 节点保存向前指针,temp 保存向后的临时指针 +//如果不保存 前向,后向指针,那在头插法使用过程中,未免破坏原链条,需要new ListNode复制节点,内存开销大.不破坏原链条,是本题的难点 ```java -public ListNode reverseList(ListNode head) { - ListNode pre = null, p = head; - while (p != null) { - // 保存当前head.Next节点,防止重新赋值后被覆盖 - // 一轮之后状态:nil<-1 2->3->4 - // prev p - ListNode temp = p.next; - p.next = pre; - // pre 移动 - pre = p; - // p 移动 - p = temp; +/** + * 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 res=new ListNode(-1); + ListNode p=head; + ListNode beh; + while(p!=null){ + //保存p的后继节点 + beh=p.next; + //把p头插入res + p.next=res.next; + res.next=p; + //p置位 + p=beh; + } + return res.next; } - return pre; } ``` @@ -208,6 +221,7 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ```java public ListNode middleNode(ListNode head) { ListNode p = head; + // fast如果初始化为head.Next则能求出第一个中间节点! ListNode q = head; while (q != null && q.next != null) { p = p.next; @@ -249,6 +263,60 @@ public void reorderList(ListNode head) { } ``` +我的解答(完整版!time:击败99.99% 空间:击败25.82%): +``` +/** + * 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 void reorderList(ListNode head) { + ListNode fast=head; + ListNode mid=head; + while(fast!=null&&fast.next!=null){ + fast=fast.next.next; + mid=mid.next; + } + ListNode head1=this.reverseList(mid.next); + //很关键,没有这句话,就报:Found cycle in the ListNode + mid.next=null; + ListNode temp; + ListNode temp1; + ListNode p=head; + while(head1!=null){ + temp=p.next; + temp1=head1.next; + + head1.next=p.next; + p.next=head1; + + p=temp; + head1=temp1; + } + } + public ListNode reverseList(ListNode head){ + ListNode res=new ListNode(-1); + ListNode p=head; + ListNode beh; + while(p!=null){ + beh=p.next; + p.next=res.next; + res.next=p; + + p=beh; + } + return res.next; + } +} + +``` + #### 回文链表