真难,不过还是要冲冲冲!!!
这个题目昨天做了没有做出来,今天再试试。直接看答案吧。
7. 链表求和
今天看到题解做出来了,感觉思路也是一般,可以接受。用到了栈,还有创建新链表的思路。加强了栈相关知识点的学习。
/**
* 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 addTwoNumbers(ListNode l1, ListNode l2) {
//用栈来解决
Deque<Integer> stack1 = new ArrayDeque<Integer>();
Deque<Integer> stack2 = new ArrayDeque<Integer>();
//将链表的数字压入栈中,因为栈是先进后出的。
while(l1 != null){
stack1.push(l1.val);
l1 = l1.next;
}
while(l2 != null){
stack2.push(l2.val);
l2 = l2.next;
}
int carry = 0;//因为要进位,所以用这个变量存储进位的数字。
ListNode ans = null;
while(!stack1.isEmpty() || !stack2.isEmpty()|| carry != 0){//加上cur != 0,可以判断是不是到最高位了
//如果栈没有空,就开始取出数值。
int a = stack1.isEmpty() ? 0 : stack1.pop();
int b = stack2.isEmpty() ? 0 : stack2.pop();
//出栈之后,进行相加操作。
int cur = a + b + carry;
carry = cur / 10;
cur = cur % 10;//得到个位数,进行存储。
ListNode currnode = new ListNode(cur);//直接创建一个节点。
//这个操作挺六,可以记好怎么操作的。就是如何将一个节点链接成一个链表。并且新创建的节点,在已经创建的节点之间。1-->2-->3-->4,先创建4,后创建3,然后依次链接。
currnode.next = ans;
ans = currnode;
}
return ans;
}
}
8. 回文链表
看方法就知道,我先使用了复制链表的方法,然后又使用了链表反转的方法。最后再判断。
/**
* 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 boolean isPalindrome(ListNode head) {
//先将链表复制,然后再将链表反转,比较两者的值是否一致。
ListNode head1 = duplicLinkedList(head);
ListNode head2 = reverseLinkedList(head);//此函数改变了head的结构。
// System .out.println("head.val = " + head.val);
//遍历两个链表
// while(head1 != null){
// System.out.println(head1.val + " ");
// head1 = head1.next;
// }
// System.out.println("======================");
// while(head2 != null){
// System.out.println(head2.val + " ");
// head2 = head2.next;
// }
while(head1 != null && head2 != null){
// System .out.println("head1.val = " + head1.val);
// System .out.println("head2.val = " + head2.val);
// System .out.println("head2.next = " + head2.next.val);
if( head1.val == head2.val){
head1 = head1.next;
head2 = head2.next;
}else{
return false;
}
}
return true;
}
//定义复制方法
public ListNode duplicLinkedList(ListNode head){
// return head;//这样是不行的,我们需要创建新的链表
ListNode head1 = null;
int i = 0;
ListNode head2 = new ListNode(0,head);
ListNode temp = head2;
while(head != null){
head1 = new ListNode(head.val);
temp.next = head1;
temp = head1;
head = head.next;
}
// return head1;//我知道了,这样返回的是最后一个节点,不是链表的头节点。
return head2.next;
}
//定义反转方法
public ListNode reverseLinkedList(ListNode head){
//反转链表,用哑节点法。在前边插入一个节点,这个是两两交换的方法。
//单节点是头插法。
ListNode pre = null;
ListNode curr = head;
//用tempNode进行操作。
while(curr != null){
//我们需要用到cur.next,所以需要进行存储
ListNode temp = curr.next;
curr.next = pre;
//移动pre和cur节点
pre = curr;
curr = temp;
}
return pre;//这里返回的就是整个链表的头节点。
}
}
这个代码是找到中间的节点,如果是奇数就是中间的,如果是偶数,就是前一半的最后一个节点。在进行链表反转的时候,需要用slow.next。即:reverseLinkedList(slow.next);
private ListNode endOfFirstHalf(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/palindrome-linked-list/solution/hui-wen-lian-biao-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
今天又是只做两道题目,真的慢,只能刷两百题目了吧。先慢慢学吧。把每一个数据结构和算法都看看。先不急。。。