删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。
示例:
给定一个链表: 1->2->3->4->5, 和 n = 2.
当删除了倒数第二个节点后,链表变为 1->2->3->5.
说明:给定的 n 保证是有效的。
两趟遍历:
1)为了保证只有头结点存在时(n=1),也能顺利删除,需要加一个pre节点指向头结点;
2)删除倒数第n个节点,我们需要找到的是倒数第n-1个节点,将其指针指向其后第二个节点;
3)第一趟遍历找到链表的长度len;删除倒数第n个节点,即删除正数第len-n+1个节点;所以我们需要找到正数第len-n个节点,将其指针指向其后第二个节点即可。
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pre = new ListNode(0);
pre.next = head;
// 链表的长
int len = 0;
while (head != null){
len ++;
head = head.next;
}
// 倒数第n个节点即正数第len-n+1个节点, 需要找到正数第len-n个节点, 将其指针指向儿子的儿子
ListNode node = pre;
int ind = 0;
while (ind < len - n){
node = node.next;
ind ++;
}
node.next = node.next.next;
return pre.next;
}
一趟遍历:
我们可以使用两个指针而不是一个指针。第一个指针从列表的开头向前移动 n+1 步,而第二个指针将从列表的开头出发。现在,这两个指针被 n 个结点分开。我们通过同时移动两个指针向前来保持这个恒定的间隔,直到第一个指针到达最后一个结点。此时第二个指针将指向从最后一个结点数起的第 n 个结点。我们重新链接第二个指针所引用的结点的 next 指针指向该结点的下下个结点。
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode pre = new ListNode(0);
pre.next = head;
ListNode left = pre;
ListNode right = pre;
int dis = 0;
while (right != null){
dis ++;
right = right.next;
if(dis > n + 1){
left = left.next;
}
}
left.next = left.next.next;
return pre.next;
}
重排链表(https://leetcode-cn.com/problems/reorder-list/)
给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:
给定链表 1->2->3->4, 重新排列为 1->4->2->3.
示例 2:
给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.
转换成ArrayList
链表的缺点就是不能随机存储,当我们想取末尾元素的时候,只能从头遍历一遍,很耗费时间。第二次取末尾元素的时候,又得遍历一遍。
所以先来个简单粗暴的想法,把链表存储到线性表中,然后用双指针依次从头尾取元素即可。
public void reorderList(ListNode head) {
List<ListNode> nodes = new ArrayList<>();
while (head != null) {
nodes.add(head);
head = head.next;
}
ListNode dummyNode = new ListNode();
ListNode curr = dummyNode;
int i = 0;
int j = nodes.size() - 1;
for (; i < j; i++, j--) {
curr.next = nodes.get(i);
curr = curr.next;
curr.next = nodes.get(j);
curr = curr.next;
}
// 奇数个节点
if(i == j){
curr.next = nodes.get(i);
curr = curr.next;
}
// 避免环的存在
curr.next = null;
head = dummyNode.next;
}
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;
}
}
链表平分、逆序、合并
主要是三步,举个例子。
1 -> 2 -> 3 -> 4 -> 5(奇数)
1 -> 2 -> 3 -> 4 -> 5 -> 6(偶数)
第一步,将链表平均分成两半
1 -> 2 -> 3和4 -> 5(奇数)
1 -> 2 -> 3 和 4->5 -> 6(偶数)
第二步,将后半部分链表逆序
1 -> 2 -> 3和5 -> 4(奇数)
1 -> 2 -> 3 和6 -> 5-> 4(偶数)
第三步,依次连接两个链表
1 -> 5->2 -> 4->3 (奇数)
1 -> 6 -> 2 -> 5 -> 3 -> 4(偶数)
第一步找中点的话,快慢指针。快指针一次走两步,慢指针一次走一步,当快指针走到终点的话,慢指针会刚好到中点。如果节点个数是偶数的话,slow 走到的是靠左端点(或靠右断点,取决于快指针的开始位置),利用这一点,我们可以把奇数和偶数的情况合并,不需要分开考虑。
第二步链表逆序的话,有迭代和递归的两种方式,迭代的话主要利用两个指针,依次逆转。
第三步的话就很简单了,两个指针分别向后移动就可以。
public void reorderList2(ListNode head) {
if(head == null || head.next == null){
return;
}
// 快慢指针找到中间节点
ListNode fast = head.next; // fast指针从head.next开始,偶数节点情况下,slow最后指向偏左端点;fast指针从head开始,偶数节点情况下,slow最后指向偏右端点;奇数节点情况下,slow都刚好指向中间节点
ListNode slow = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
// 翻转后半条链, 中间节点往右属于后半链(不包括中间节点)
ListNode secHead = slow.next;
slow.next = null;
secHead = reverse(secHead);
ListNode firstHead = head;
while (firstHead != null && secHead != null){
ListNode tempHead = firstHead.next;
firstHead.next = secHead;
ListNode tempSecHead = secHead.next;
secHead.next = tempHead;
firstHead = tempHead;
secHead = tempSecHead;
}
}
public ListNode reverse(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode tail = head;
head = head.next;
tail.next = null;
while (head != null){
ListNode temp = head.next;
head.next = tail;
tail = head;
head = temp;
}
return tail;
}
环形链表 II(https://leetcode-cn.com/problems/linked-list-cycle-ii/)
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
说明:不允许修改给定的链表。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:tail connects to node index 1
解释:链表中有一个环,其尾部连接到第二个节点。
进阶:
你是否可以不用额外空间解决此题?
快慢指针(Floyd 算法)
为了避免使用额外空间,这里不用Hash表来实现,使用Floyd 算法实现。
Floyd 的算法被划分成两个不同的 阶段 。在第一阶段,使用快慢指针找出列表中是否有环,如果没有环(快慢指针没有相遇),可以直接返回 null 并退出。否则,记录下快慢指针相遇的节点。第二阶段,使用 相遇节点 来找到环的入口。
阶段 1
这里我们初始化两个指针 - 快指针和慢指针。我们每次移动慢指针一步、快指针两步,直到快指针无法继续往前移动。如果在某次移动后,快慢指针指向了同一个节点(相遇),我们就返回它。否则,我们继续,直到 while 循环终止且没有返回任何节点,这种情况说明没有成环,我们返回 null 。
阶段2
我们初始化额外的两个指针: ptr1 ,指向链表的头, ptr2 指向相遇点。然后,我们每次将它们往前移动一步,直到它们相遇,它们相遇的点就是环的入口,返回这个节点。
阶段2的正确性证明:
如下图所示,快慢指针第一次相遇的节点为node h,环的入口为node 0,两个节点将环分成长度为a和b的两段。
由上面的结论可以得出,当ptr1指针从head节点开始走,同时ptr2指针从第一次相遇节点(node h)开始走,则有:当ptr1节点走完F的距离到达环的入口点(node 0)处时,ptr2指针刚好走过b(外加整数圈)到达入口点,所以两者相遇的节点就是入口点。
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
// 第一阶段:找到第一次相遇点
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
break;
}
}
if(fast == null || fast.next == null){
return null;
}
// 第二阶段:开始找环的入口点
while (head != fast){
head = head.next;
fast = fast.next;
}
return head;
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
排序链表(https://leetcode-cn.com/problems/sort-list/)
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
示例 1:
输入: 4->2->1->3
输出: 1->2->3->4
示例 2:
输入: -1->5->3->4->0
输出: -1->0->3->4->5
public ListNode sortList(ListNode head) {
if(head != null && head.next != null){
// 找到一半的位置
ListNode fast = head.next; // fast从head.next开始
ListNode slow = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
ListNode medium = slow.next;
slow.next = null;
// 递归划分
ListNode head1 = sortList(head);
ListNode head2 = sortList(medium);
// 合并
return merge(head1, head2);
}
return head;
}
public ListNode merge(ListNode head1, ListNode head2){
ListNode dummyHead = new ListNode(0);
ListNode head = dummyHead;
while (head1 != null && head2 != null){
if(head1.val < head2.val){
head.next = head1;
head1 = head1.next;
}else {
head.next = head2;
head2 = head2.next;
}
head = head.next;
}
if(head1 != null){
head.next = head1;
}
if(head2 != null){
head.next = head2;
}
return dummyHead.next;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
旋转链表(https://leetcode-cn.com/problems/rotate-list/)
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。
示例 1:
输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL
解释:
向右旋转 1 步: 5->1->2->3->4->NULL
向右旋转 2 步: 4->5->1->2->3->NULL
示例 2:
输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL
解释:
向右旋转 1 步: 2->0->1->NULL
向右旋转 2 步: 1->2->0->NULL
向右旋转 3 步: 0->1->2->NULL
向右旋转 4 步: 2->0->1->NULL
链表闭合(头尾相连)
1)遍历链表,得到链表长度的同时,将链表头尾相连;
2)利用链表长度和k,得到闭合链表的切割位置。
public ListNode rotateRight(ListNode head, int k) {
if(k == 0 || head == null || head.next == null){
return head;
}
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
// 闭环, 得到链表的长度
int len = 1;
ListNode node = head;
while (node.next != null){
len ++;
node = node.next;
}
node.next = dummyNode.next;
k = k % len;
// 找切割点(即新头的前一个节点)
for(int i=0; i<len-k-1; i++){
head = head.next;
}
ListNode ret = head.next;
head.next = null;
return ret;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
反转链表 II(https://leetcode-cn.com/problems/reverse-linked-list-ii/)
反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
说明:
1 ≤ m ≤ n ≤ 链表长度。
示例:
输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL
迭代法:
在看具体算法之前,有必要先弄清楚链接反转的原理以及需要哪些指针。举例而言,有一个三个不同结点组成的链表 A → B → C,需要反转结点中的链接成为 A ← B ← C。
假设我们有两个指针,一个指向结点 A,一个指向结点 B。 分别记为 prev 和 cur。则可以用这两个指针简单地实现 A 和 B 之间的链接反转:
cur.next = prev
这样做唯一的问题是,没有办法继续下去,换而言之,这样做之后就无法再访问到结点 C。因此,我们需要引入第三个指针,用于帮助反转过程的进行。因此,我们不采用上面的反转方法,而是:
third = cur.next
cur.next = prev
prev = cur
cur = third
迭代 地进行上述过程,即可完成问题的要求。下面来看看算法的步骤。
1.首先找到需要翻转开始位置的前一个节点,记作pre
2.在m <= index<= n 的时候对链表进行翻转,记作rev
3.连接三部分,pre + rev + index>n的剩余节点,如果m = 1,那么只有rev + index>n的剩余节点
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || head.next == null || m == n) {
return head;
}
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
// head为翻转开始节点的前一个节点
if(m == 1){
head = dummyHead;
}else {
int index = 1;
while (index++ < m - 1) {
head = head.next;
}
}
// 翻转开始节点
ListNode start = head.next;
ListNode back = start;
ListNode front = back.next;
ListNode temp = null;
int index = 0;
while (index++ < n - m && front != null) {
temp = front.next;
// 翻转
front.next = back;
// 遍历一步
back = front;
front = temp;
}
// 前后接入
head.next = back;
start.next = front;
return dummyHead.next;
}
当然此题也可以由递归算法解决,通过改变节点的值,而非改变链接的指向;针对此题,递归算法相对比较难以理解,不推荐使用。
private boolean stop = false;
private ListNode left;
public ListNode reverseBetween(ListNode head, int m, int n) {
left = head;
recursion(head, m, n);
return head;
}
public void recursion(ListNode right, int m, int n){
if(n <= 1){
return;
}
right = right.next;
if(m > 1){
left = left.next;
}
recursion(right, m-1, n-1);
if(left == right || right.next == left){
stop = true;
}
if(!stop){
int temp = left.val;
left.val = right.val;
right.val = temp;
left = left.next;
}
}
K 个一组翻转链表
public ListNode reverseKGroup(ListNode head, int k) {
if (k == 1 || head == null || head.next == null) {
return head;
}
ListNode tail = head;
int left = k;
while (left > 1 && tail != null) {
tail = tail.next;
left--;
}
if (tail != null) {
// 翻转
ListNode pre = head;
ListNode cur = head.next;
while (pre != tail) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
head.next = reverseKGroup(cur, k);
return tail;
} else {
return head;
}
}
两数相加 II
链表反转
1)链表反转:preNode和currentNode
2) while循环条件中注意:carry(余数)不为0的情况
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// 翻转l1
ListNode l1Head = reverse(l1);
// 翻转l2
ListNode l2Head = reverse(l2);
// 相加
ListNode preNode = null;
int carry = 0;
while (l1Head != null || l2Head != null || carry != 0){
int value1 = l1Head != null ? l1Head.val : 0;
int value2 = l2Head != null ? l2Head.val : 0;
int value = value1 + value2 + carry;
carry = value / 10;
value = value % 10;
ListNode currentNode = new ListNode(value);
currentNode.next = preNode;
preNode = currentNode;
l1Head = l1Head != null ? l1Head.next : null;
l2Head = l2Head != null ? l2Head.next : null;
}
return preNode;
}
public ListNode reverse(ListNode head){
if(head == null || head.next == null){
return head;
}
ListNode preNode = null;
ListNode currentNode = head;
ListNode temp = null;
while (currentNode != null){
temp = currentNode.next;
currentNode.next = preNode;
preNode = currentNode;
currentNode = temp;
}
return preNode;
}
栈
通过倒序相加,应该立即联想到栈
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
while (l1 != null){
stack1.push(l1.val);
l1 = l1.next;
}
while (l2 != null){
stack2.push(l2.val);
l2 = l2.next;
}
int carry = 0;
ListNode preNode = null;
while (!stack1.isEmpty() || !stack2.isEmpty() || carry != 0){
int value1 = 0;
if(!stack1.isEmpty()){
value1 = stack1.pop();
}
int value2 = 0;
if(!stack2.isEmpty()){
value2 = stack2.pop();
}
int sum = value1 + value2 + carry;
int value = sum % 10;
carry = sum / 10;
ListNode currentNode = new ListNode(value);
currentNode.next = preNode;
preNode = currentNode;
}
return preNode;
}
删除排序链表中的重复元素 II
三指针法
1)cur指针:当前遍历节点
2)pre指针:当前指针的前一个节点,改变此节点的next链接,即起到删除作用
3)nextJump指针:遍历到最后一个与cur节点重复的节点
技巧:可以使用DummyNode,处理head就是重复节点的情形;
public ListNode deleteDuplicates(ListNode head) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode cur = head;
ListNode pre = dummyHead;
while (cur != null){
if(cur.next == null || (cur.next != null && cur.val != cur.next.val)){
pre = cur;
cur = cur.next;
}else {
ListNode nextJump = cur.next;
while (nextJump.next != null && nextJump.next.val == cur.val){
nextJump = nextJump.next;
}
pre.next = nextJump.next;
cur = nextJump.next;
}
}
return dummyHead.next;
}
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
复制带随机指针的链表
递归解法
此题的关键在于原链表和clone链表的节点对应关系的存储,防止无线循环下去,使用一个HashMap存储原链表的节点和对应的clone节点的关系,这样在赋值next指针和random指针的时候,避免重复new节点。
private Map<Node, Node> cloneMap = new HashMap<>();
public Node copyRandomList(Node head) {
if(head == null){
return null;
}
if(cloneMap.containsKey(head)){
return cloneMap.get(head);
}
Node newHead = new Node(head.val);
cloneMap.put(head, newHead);
newHead.next = copyRandomList(head.next);
newHead.random = copyRandomList(head.random);
return newHead;
}
迭代解法
关键点与上面一致,使用HashMap保存原节点和clone节点的对应关系;
1)可先将所有节点clone出来,即先给next指针赋值;
2)然后再给所有节点的random节点赋值;
public Node copyRandomList(Node head) {
if(head == null){
return null;
}
Map<Node, Node> cloneMap = new HashMap<>();
Node dummyHead = new Node(0);
Node newPtr = dummyHead;
Node oriPtr = head;
while (oriPtr != null){
Node newNode = new Node(oriPtr.val);
cloneMap.put(oriPtr, newNode);
newPtr.next = newNode;
oriPtr = oriPtr.next;
newPtr = newPtr.next;
}
// 设置random指针
oriPtr = head;
newPtr = dummyHead.next;
while (oriPtr != null){
if(oriPtr.random != null){
newPtr.random = cloneMap.get(oriPtr.random);
}
oriPtr = oriPtr.next;
newPtr = newPtr.next;
}
return dummyHead.next;
}
更加清晰的解法:
public Node copyRandomList(Node head) {
HashMap<Node,Node> map = new HashMap<>(); //创建HashMap集合
Node cur=head;
//复制结点值
while(cur!=null){
//存储put:<key,value1>
map.put(cur,new Node(cur.val)); //顺序遍历,存储老结点和新结点(先存储新创建的结点值)
cur=cur.next;
}
//复制结点指向
cur = head;
while(cur!=null){
//得到get:<key>.value2,3
map.get(cur).next = map.get(cur.next); //新结点next指向同旧结点的next指向
map.get(cur).random = map.get(cur.random); //新结点random指向同旧结点的random指向
cur = cur.next;
}
//返回复制的链表
return map.get(head);
}
奇偶链表
public ListNode oddEvenList(ListNode head) {
if(head == null){
return null;
}
ListNode odd = head;
ListNode even = odd.next;
ListNode evenStart = even;
while (even != null && even.next != null){
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenStart;
return head;
}
分隔链表
双指针法
public ListNode partition(ListNode head, int x) {
if(head == null){
return null;
}
ListNode smallDummy = new ListNode(0);
ListNode smallPtr = smallDummy;
ListNode bigDummy = new ListNode(0);
ListNode bigPtr = bigDummy;
while (head != null){
if(head.val < x){
smallPtr.next = head;
smallPtr = smallPtr.next;
}else {
bigPtr.next = head;
bigPtr = bigPtr.next;
}
head = head.next;
}
bigPtr.next = null; // 关键:否则会造成循环
smallPtr.next = bigDummy.next;
return smallDummy.next;
}