1、
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
限制:0 <= 节点个数 <= 5000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
/**
* 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 H = head;
ListNode T = null;
while(H!=null){
ListNode F = new ListNode(H.val);
if(H!=head){
F.next = T;
}
T = F;
H=H.next;
}
return T;
}
}
//递归
class Solution {
public ListNode reverseList(ListNode head) {
// base case
if (head == null || head.next == null) {
return head;} //如果head是最后一个元素或无元素返回head
ListNode last = reverseList(head.next); //缩小递归数据
head.next.next = head; //反转办法
head.next = null;
return last;
}
}
总结: 递归解题基本方案
- 找到返回方案(也就是确定递归什么时候结束)
- 确定方法怎么一层层缩小
- 问题一般话(最简单数据处理方案)
2、
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
示例1:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
限制:
0 <= 链表长度 <= 1000
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
//迭代
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode H = new ListNode();
ListNode F = H;
while(l1 != null && l2 != null){
if(l1.val<=l2.val ){
F.next = l1;
l1=l1.next;
}
else{
F.next = l2;
l2=l2.next;
}
F = F.next;
}
F.next = (l1 == null ? l2:l1);
return H.next;
}
}
总结: 实参与虚参要分清,对于对象而言 new过之后才代表创建了新对象。
ListNode H = new ListNode();
ListNode F = H;
其中F与H是指向同一个地址
3、
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形式:[3,4,5]) 返回的结点值为 3 。
(测评系统对该结点序列化表述是 [3,4,5])。 注意,我们返回了一个 ListNode 类型的对象 ans,这样: ans.val =3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next =
NULL.来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/middle-of-the-linked-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public ListNode middleNode(ListNode head) {
ListNode s = head;
ListNode f = s;
int i = 1;
while(s !=null && s.next!=null ){
s = s.next;
i++;
}
if(i%2==0){
i = i/2+1;
}
else{
i = (i+1)/2;
}
while(i!=1){
f = f.next;
i--;
}
return f;
}
}
快慢指针(快指针比慢指针快一倍)
class Solution {
public ListNode middleNode(ListNode head) {
ListNode p = head, q = head;
while (q != null && q.next != null) {
q = q.next.next;
p = p.next;
}
return p;
}
}
总结: 1.判断链表中的环
2.得到链表中的中间值
4、
给定两个(单向)链表,判定它们是否相交并返回交点。请注意相交的定义基于节点的引用,而不是基于节点的值。换句话说,如果一个链表的第k个节点与另一个链表的第j个节点是同一节点(引用完全相同),则这两个链表相交。
示例 1:
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA
= 2, skipB = 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。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNo