Merge pull request #858 from hailincai/master

Update 设计链表和翻转链表
This commit is contained in:
程序员Carl
2021-10-22 14:59:14 +08:00
committed by GitHub
2 changed files with 25 additions and 6 deletions

View File

@ -164,6 +164,25 @@ class Solution {
} }
``` ```
```java
// 从后向前递归
class Solution {
ListNode reverseList(ListNode head) {
// 边缘条件判断
if(head == null) return null;
if (head.next == null) return head;
// 递归调用,翻转第二个节点开始往后的链表
ListNode last = reverseList(head.next);
// 翻转头节点与第二个节点的指向
head.next.next = head;
// 此时的 head 节点为尾节点next 需要指向 NULL
head.next = null;
return last;
}
}
```
Python迭代法 Python迭代法
```python ```python
#双指针 #双指针

View File

@ -282,12 +282,12 @@ Java
```Java ```Java
//单链表 //单链表
class ListNode { class ListNode {
int val; int val;
ListNode next; ListNode next;
ListNode(){} ListNode(){}
ListNode(int val) { ListNode(int val) {
this.val=val; this.val=val;
} }
} }
class MyLinkedList { class MyLinkedList {
//size存储链表元素的个数 //size存储链表元素的个数