文章目录
常用技巧与操作
技巧
- 多画图!!直观且形象,便于理解指向关系。
- 引入虚拟头结点,可以使代码统一,便于处理边界情况,也方便对链表操作
- 大胆创建局部变量。
举个例子:在双向链表中插入新节点,需要写四行代码,并且顺序不能随便改,要保证链表不断开,思路很复杂。而引入了局部变量就价低了思考成本,避免链表断开的问题。

4. 快慢双指针 是非常经典的解法,可以解决判断是否有环、找环入口、俩链表中倒数第N个节点等问题
操作
- 创建新节点 :
new ListNode() - 尾插: 创建尾指针
tail维护链表最后一个节点,tail.next = nodetail = node - 头插:
newhead.next = head.nexthead = newhead方便进行逆序链表操作
1. 两数相加(LC2)
题目描述

解题思路
直接模拟两数相加过程
- 创建虚拟头结点
head cur1和cur2分别遍历两个链表,t保存cur1与cur2指向数的和,结果链表中尾插t的末位- 如果上一次产生了进位,剔除
t的末位,此时剩余的1表示进位,t继续+=cur1与cur2指向的数.
代码解析
java
class Solution {
ListNode head = new ListNode();
ListNode tail = head;
//尾插
private void addLast(int val){
ListNode node = new ListNode(val);
tail.next = node;
tail = node;
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode cur1 = l1;
ListNode cur2 = l2;
int t = 0;
while(cur1 != null && cur2 != null){
t += cur1.val + cur2.val;
addLast(t%10);
t /= 10;
cur1 = cur1.next;
cur2 = cur2.next;
}
while(cur1 != null){
t += cur1.val;
addLast(t%10);
t/=10;
cur1 = cur1.next;
}
while(cur2 != null){
t += cur2.val;
addLast(t%10);
t/=10;
cur2 = cur2.next;
}
if(t==1)
addLast(1);
return head.next;
}
}
注意: 如果最后t还为1,说明还有进位,要在结尾加上新节点,值为1.
拓展:链表相加二(NC40)
相加前需要把两个链表进行逆序操作,得到的结果也要逆序后返回。
java
public class Solution {
//逆序
private ListNode reverse(ListNode node){
ListNode head = new ListNode(0);
ListNode cur = node;
while(cur!=null){
ListNode newNode = new ListNode(cur.val);
newNode.next = head.next;
head.next = newNode;
cur = cur.next;
}
return head.next;
}
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
ListNode cur1 = reverse(head1);
ListNode cur2 = reverse(head2);
ListNode head = new ListNode(0);
ListNode tail = head;
int t = 0;
while(cur1 != null || cur2 != null || t!=0){
if(cur1 != null){
t += cur1.val;
cur1 = cur1.next;
}
if(cur2 != null){
t += cur2.val;
cur2 = cur2.next;
}
tail.next = new ListNode(t%10);
tail = tail.next;
t /= 10;
}
head = reverse(head.next);
return head;
}
}
