parent
13915f6a95
commit
9cd6f13e54
175
notes/面试总结.md
175
notes/面试总结.md
@ -26,6 +26,8 @@
|
||||
* [21. 调整数组顺序使奇数位于偶数前面](#21-调整数组顺序使奇数位于偶数前面)
|
||||
* [22. 链表中倒数第 K 个结点](#22-链表中倒数第-k-个结点)
|
||||
* [23. 链表中环的入口结点](#23-链表中环的入口结点)
|
||||
* [24. 反转链表](#24-反转链表)
|
||||
* [25. 合并两个排序的链表](#25-合并两个排序的链表)
|
||||
|
||||
* [参考文献](#参考文献)
|
||||
<!-- GFM-TOC -->
|
||||
@ -1790,6 +1792,179 @@ class Solution:
|
||||
if pHead in plist:
|
||||
return pHead
|
||||
```
|
||||
# 24. 反转链表
|
||||
|
||||
[NowCoder](https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||
|
||||
## 解题思路
|
||||
|
||||
### 递归
|
||||
|
||||
```java
|
||||
public ListNode ReverseList(ListNode head) {
|
||||
if (head == null || head.next == null)
|
||||
return head;
|
||||
ListNode next = head.next;
|
||||
head.next = null;
|
||||
ListNode newHead = ReverseList(next);
|
||||
next.next = head;
|
||||
return newHead;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
# -*- coding:utf-8 -*-
|
||||
# class ListNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.next = None
|
||||
class Solution:
|
||||
# 返回ListNode
|
||||
def ReverseList(self, pHead):
|
||||
# write code here
|
||||
if pHead == None or pHead.next == None:
|
||||
return pHead
|
||||
next = pHead.next
|
||||
pHead.next = None
|
||||
newHead = self.ReverseList(next)
|
||||
next.next = pHead
|
||||
return newHead
|
||||
```
|
||||
|
||||
### 迭代
|
||||
|
||||
```java
|
||||
public ListNode ReverseList(ListNode head) {
|
||||
ListNode newList = new ListNode(-1);
|
||||
while (head != null) {
|
||||
ListNode next = head.next;
|
||||
head.next = newList.next;
|
||||
newList.next = head;
|
||||
head = next;
|
||||
}
|
||||
return newList.next;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
# -*- coding:utf-8 -*-
|
||||
# class ListNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.next = None
|
||||
class Solution:
|
||||
# 返回ListNode
|
||||
def ReverseList(self, pHead):
|
||||
# write code here
|
||||
newList = ListNode(-1)
|
||||
while pHead:
|
||||
next = pHead.next
|
||||
pHead.next = newList.next
|
||||
newList.next = pHead
|
||||
pHead = next
|
||||
return newList.next
|
||||
```
|
||||
|
||||
# 25. 合并两个排序的链表
|
||||
|
||||
[NowCoder](https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337?tpId=13&tqId=11169&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||
|
||||
## 题目描述
|
||||
|
||||
<div align="center"> <img src="../pics//43f2cafa-3568-4a89-a895-4725666b94a6.png" width="500"/> </div><br>
|
||||
|
||||
## 解题思路
|
||||
|
||||
### 递归
|
||||
|
||||
```java
|
||||
public ListNode Merge(ListNode list1, ListNode list2) {
|
||||
if (list1 == null)
|
||||
return list2;
|
||||
if (list2 == null)
|
||||
return list1;
|
||||
if (list1.val <= list2.val) {
|
||||
list1.next = Merge(list1.next, list2);
|
||||
return list1;
|
||||
} else {
|
||||
list2.next = Merge(list1, list2.next);
|
||||
return list2;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
# -*- coding:utf-8 -*-
|
||||
# class ListNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.next = None
|
||||
class Solution:
|
||||
# 返回合并后列表
|
||||
def Merge(self, pHead1, pHead2):
|
||||
# write code here
|
||||
if pHead1 == None:
|
||||
return pHead2
|
||||
if pHead2 == None:
|
||||
return pHead1
|
||||
if pHead1.val <= pHead2.val:
|
||||
pHead1.next = self.Merge(pHead1.next, pHead2)
|
||||
return pHead1
|
||||
else:
|
||||
pHead2.next = self.Merge(pHead1, pHead2.next)
|
||||
return pHead2
|
||||
```
|
||||
|
||||
### 迭代
|
||||
|
||||
```java
|
||||
public ListNode Merge(ListNode list1, ListNode list2) {
|
||||
ListNode head = new ListNode(-1);
|
||||
ListNode cur = head;
|
||||
while (list1 != null && list2 != null) {
|
||||
if (list1.val <= list2.val) {
|
||||
cur.next = list1;
|
||||
list1 = list1.next;
|
||||
} else {
|
||||
cur.next = list2;
|
||||
list2 = list2.next;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
if (list1 != null)
|
||||
cur.next = list1;
|
||||
if (list2 != null)
|
||||
cur.next = list2;
|
||||
return head.next;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
# -*- coding:utf-8 -*-
|
||||
# class ListNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.next = None
|
||||
class Solution:
|
||||
# 返回合并后列表
|
||||
def Merge(self, pHead1, pHead2):
|
||||
# write code here
|
||||
head = ListNode(-1)
|
||||
cur = head
|
||||
while pHead1 and pHead2:
|
||||
if pHead1.val <= pHead2.val:
|
||||
cur.next = pHead1
|
||||
pHead1 = pHead1.next
|
||||
else:
|
||||
cur.next = pHead2
|
||||
pHead2 = pHead2.next
|
||||
cur = cur.next
|
||||
if pHead1:
|
||||
cur.next = pHead1
|
||||
if pHead2:
|
||||
cur.next = pHead2
|
||||
return head.next
|
||||
```
|
||||
|
||||
# 参考文献
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user