妙想:直接设计程序,让程序输出。
206 链表 逆序
**题目描述:**已知链表头结点,将链表逆序,不可额外申请辅助空间
思路:
实现
//给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
//
//
//
//
// 示例 1:
//
//
//输入:head = [1,2,3,4,5]
//输出:[5,4,3,2,1]
//
//
// 示例 2:
//
//
//输入:head = [1,2]
//输出:[2,1]
//
//
// 示例 3:
//
//
//输入:head = []
//输出:[]
//
//
//
//
// 提示:
//
//
// 链表中节点的数目范围是 [0, 5000]
// -5000 <= Node.val <= 5000
//
//
//
//
// 进阶:链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?
//
//
// Related Topics 链表
// 👍 1751 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *new_head = NULL;
while(head){
ListNode *next = head ->next;
head ->next = new_head;
new_head = head;
head = next;
}
return new_head;
}
};
//leetcode submit region end(Prohibit modification and deletion)
92 链表逆序(从m 到n)
思路:
//给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链
//表节点,返回 反转后的链表 。
//
//
// 示例 1:
//
//
//输入:head = [1,2,3,4,5], left = 2, right = 4
//输出:[1,4,3,2,5]
//
//
// 示例 2:
//
//
//输入:head = [5], left = 1, right = 1
//输出:[5]
//
//
//
//
// 提示:
//
//
// 链表中节点数目为 n
// 1 <= n <= 500
// -500 <= Node.val <= 500
// 1 <= left <= right <= n
//
//
//
//
// 进阶: 你可以使用一趟扫描完成反转吗?
// Related Topics 链表
// 👍 909 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
int changeList = right-left+1;
ListNode *pre_head =NULL;
ListNode *result = head;
while(head && --left){
pre_head =head;
head = head ->next;
}
ListNode *new_tail = head ;
ListNode *new_head = NULL;
while(head && changeList){
ListNode *next = head ->next;
head -> next = new_head;
new_head= head;
head = next;
changeList--;
}
new_tail->next = head;
if(pre_head){
pre_head-> next = new_head;
}
else{
result = new_head;
}
return result;
}
};
160 求链表交点
方法二方法一:
当两个链表相交时,从相交的地方一直到末尾都是相同的
//编写一个程序,找到两个单链表相交的起始节点。
//
// 如下面的两个链表:
//
//
//
// 在节点 c1 开始相交。
//
//
//
// 示例 1:
//
//
//
// 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, s
//kipB = 3
//输出:Reference of the node with value = 8
//输入解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1
//,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
//
//
//
//
// 示例 2:
//
//
//
// 输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB =
// 1
//输出:Reference of the node with value = 2
//输入解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4
//]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
//
//
//
//
// 示例 3:
//
//
//
// 输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
//输出:null
//输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而
// skipA 和 skipB 可以是任意值。
//解释:这两个链表不相交,因此返回 null。
//
//
//
//
// 注意:
//
//
// 如果两个链表没有交点,返回 null.
// 在返回结果后,两个链表仍须保持原有的结构。
// 可假定整个链表结构中没有循环。
// 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
//
// Related Topics 链表
// 👍 1137 👎 0
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {} // 构造器
* };
*/
// 起名字不要起有争议的名字
// 返回链表长度
int list_length(ListNode *head){
int len=0;
while(head){
len++;
head = head->next;
}
return len;
}
// 移动长的链表
ListNode *get_forward_list(int long_len,int short_len, ListNode *head)
{
int dist= long_len - short_len;
while(head && dist){
head =head->next;
dist--;
}
return head;
}
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA= list_length(headA);
int lenB = list_length(headB);
if( lenA > lenB){
headA = get_forward_list(lenA,lenB,headA);
}else{
headB = get_forward_list(lenB,lenA,headB);
}
while(headB && headA){
if(headA==headB){
return headA;
}
headA = headA-> next;
headB = headB ->next;
}
return NULL;
}
};
//leetcode submit region end(Prohibit modification and deletion)
141 链表求环
使用set
快慢指针
86 分割链表
刷题总结
起名字不要起有争议的名字