24. 两两交换链表中的节点
思路:
需要保存四个指针,作为一次循环。每次就翻转一次局部的链表。保证每次开始和结束翻转的状态一致,就可以进行循环了。
代码
/**
* 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 swapPairs(ListNode head) {
ListNode pre = new ListNode();
pre.next = head;
ListNode cur = pre;
//cur 1 2 3
while(cur.next != null && cur.next.next != null) {
ListNode next3 = cur.next.next.next;
ListNode next2 = cur.next.next;
ListNode next1 = cur.next;
cur.next = next2;
next2.next = next1;
next1.next = next3;
cur = cur.next.next;
}
return pre.next;
}
}
19.删除链表的倒数第N个节点
思路
双指针法,让一个指针先行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) {
//定义一个伪节点,用于返回结果
ListNode dumpy = new ListNode(0);
dumpy.next = head;
//定义一个快指针,指向伪节点,用于遍历链表
ListNode prev = dumpy;
//定一个慢指针,
ListNode tail = dumpy;
//让快指针先移动 n 步
while(n >0){
prev = prev.next;
n--;
}
//只要快指针不指向空,就继续循环
while(prev.next !=null){
//让快慢指针同时移动
tail = tail.next;
prev = prev.next;
}
//这时慢指针移动到的位置就是,要删除节点的前一个节点
//所以只要删除当前节点的下一个节点
tail.next = tail.next.next;
//返回为指针指向的头节点
return dumpy.next;
}
}
面试题 02.07. 链表相交
思路
先计算出两个链表的长度,然后使用双指针法分别指向这两个链表,将较长的数组的指针移动到跟较短的数组对齐,然后比较是否相同,如果相同则返回,不相同,则继续移动。
可能出错的点:注意比较的是节点是否相等,而不是节点值是否相等!!!
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA == null || headB == null){
return null;
}
ListNode tempA = headA;
ListNode tempB = headB;
int lenA = 0;
int lenB = 0;
while(tempA != null){
tempA = tempA.next;
lenA++;
}
while(tempB != null){
tempB = tempB.next;
lenB++;
}
tempB = headB;
tempA = headA;
if(lenA>lenB){
int n = lenA - lenB;
while(n-- > 0){
tempA = tempA.next;
}
}else{
int n = lenB - lenA;
while(n-- > 0){
tempB = tempB.next;
}
}
while(tempB != null){
if(tempA == tempB){
return tempA;
}
tempA = tempA.next;
tempB = tempB.next;
}
return null;
}
}
142.环形链表II
思路
这题较难,自己做过一遍还是没什么思路,题解思路:
1、判断链表是否有环
快慢指针法,从头节点出发,fast每次移动两个节点,slow指针每次移动1个节点。
如果他们相遇,就说明有环:fast一定先入环,如果他们相遇,一定是在环里。相对于slow来说,fast是一个一个节点靠近slow的。
2、如果有环,如何找到环的入口
按照公式x = n(y+z)-y=(n-1)(y+z)+z
双指针法:让一个节点从头节点从出发,一个节点从相遇节点出发,相遇的点即为环形入口节点。
代码:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
int flag = 0;
while(fast != null && fast.next !=null){
fast = fast.next.next;
slow = slow.next;
if(slow == fast){
flag = 1;
break;
}
}
if(flag == 0){
return null;
}
ListNode temp1= head;
while(temp1 != slow){
temp1 = temp1.next;
slow = slow.next;
}
return slow;
}
}