今天继续刷链表
首先回顾以下昨天两道题目,解法
1.第一道题目是将找到两个
1. 找出两个链表的交点
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode l1 = headA, l2 = headB;
while (l1 != l2) {
l1 = (l1 == null) ? headB : l1.next;
l2 = (l2 == null) ? headA : l2.next;
}
return l1;
}
2. 链表反转
递归思想:(有点小难)
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode next = head.next;
ListNode newHead = reverseList(next);
next.next = head;
head.next = null;
return newHead;
}
=========================================================================
开始新题目:
3.3. 归并两个有序的链表
又是递归。。。
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
} else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
迭代方法也很难。。。
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);
ListNode prev = prehead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
prev.next = l1 == null ? l2 : l1;
return prehead.next;
}
}
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
再刷一道题目吧,看了一个小时。。。
4. 从有序链表中删除重复节点
我的解法:
/**
* 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 deleteDuplicates(ListNode head) {
//先考虑特殊情况
if(head == null || head.next == null){
return head;
}
ListNode node1 = head, node2 = head.next;
while(node2 != null){
while( node2 != null && node1 != null && node1.val == node2.val){
node2 = node2.next;
}//结束循环,代表找到了不一样的的节点
node1.next = node2;
node1 = node2;
if(node2 != null){
node2 = node2.next;
}
}
return head;
}
}
官网:
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) {
return head;
}
ListNode cur = head;
while (cur.next != null) {
if (cur.val == cur.next.val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
return head;
}
}
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-list/solution/shan-chu-pai-xu-lian-biao-zhong-de-zhong-49v5/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
5. 删除链表的倒数第 n 个节点
/**
* 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 removeNthFromEnd(ListNode head, int n) {
// //链表的反转,先反转再删除。
// //倒数第n个,从前边往后数,就是第length-n+1
// int length = getLength(head);
// // if(n == 0 || head == null ){
// // return head;
// // }
// ListNode cur = new ListNode(-1, head);
// ListNode node0 = cur;
// // int i = 1;//计数
// // if(length == 1 && n == 1){
// // return null;
// // }
// // while(i != (length-n+1)){//但退出循环时候,node0指向节点的后一个位置就是要删除的节点
// // node0 = node0.next;
// // i++;
// // }
// for(int i = 1; i < length - n + 1; ++i){
// node0 = node0.next;
// }
// // ListNode node1 = node0.next.next;
// node0.next = node0.next.next;
// // node1 = null;
// return head;
//===================================
ListNode dummy = new ListNode(0, head);
int length = getLength(head);
ListNode cur = dummy;
// for (int i = 1; i < length - n + 1; ++i) {
// cur = cur.next;
// }
int i = 1;
while(i != (length - n + 1)){
cur = cur.next;
i++;
}
cur.next = cur.next.next;
// ListNode ans = dummy.next;
// return ans;
return dummy.next;
}
//写一个方法得到链表的长度
public int getLength(ListNode head){
int length = 0;
while(head != null){
length++;
head = head.next;
}
return length;
}
// }
// public ListNode reverseLinkedList(ListNode head){
// ListNode node1 = null;
// ListNode node2 = head;
// if(head == null){
// return head;
// }
// while(node2 != null){
// ListNode node3 = null;
// node3 = node2.next;
// node2.next = node1;
// node1 = node2;
// node2 = node3;
// }
// return node1;
// }
}
就是最后返回值地方出现了问题,所以不能从中间节点返回,要从头节点。应该是内部的代码判定的时候的原因。
下班!!!加油啊,太难了,但是不能放弃。。。
不从事研发行业,将来会后悔的。所以不能怕困难。
现在努力还来得及,加油。