24. 两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换
输入:head = [1,2,3,4]
输出:[2,1,4,3]
示例 2: 输入:head = []
输出:[]
示例 3: 输入:head = [1]
输出:[1]
package days0605_60dyas.a;
public class Lian2 {
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
head.next = node2;
node2.next = node3;
node3.next = node4;
ListNode current = head;
while (current != null) {
System.out.print(current.val + " ");
current = current.next;
}
System.out.println("===");
ListNode lian2=new ListNode();
lian2=swapPairs(head);
ListNode current2=lian2;
while (current2 != null) {
System.out.print(current2.val + " ");
current2 = current2.next;
}
}
public static ListNode swapPairs(ListNode head) {
ListNode dummyhead = new ListNode(-1);
dummyhead.next = head;
ListNode cur = dummyhead;
ListNode temp;
ListNode firstnode;
ListNode secondnode;
while (cur.next != null && cur.next.next != null) {
temp = cur.next.next.next;
firstnode = cur.next;
secondnode = cur.next.next;
cur.next = secondnode;
secondnode.next = firstnode;
firstnode.next = temp;
cur = firstnode;
}
return dummyhead.next;
}
}
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5]
package days0605_60dyas.a;
public class Lian3 {
public static void main(String[] args) {
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
removeNumFromEnd(node1,2);
ListNode cur = node1;
while (cur != null) {
System.out.println(" __" + cur.val);
cur = cur.next;
}
}
public static ListNode removeNumFromEnd(ListNode head, int n) {
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode fastIndex = dummyNode;
ListNode slowIndex = dummyNode;
for (int i = 0; i <= n; i++) {
fastIndex = fastIndex.next;
}
while (fastIndex != null) {
fastIndex = fastIndex.next;
slowIndex = slowIndex.next;
}
if (slowIndex != null) {
slowIndex.next = slowIndex.next.next;
}
return dummyNode.next;
}
}
面试题 02.07. 链表相交
给你两个单链表的头节点 headA
和 headB
,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null
。
图示两个链表在节点 c1
开始相交:
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
示例 1:
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 输出:Intersected at '8'
package days0605_60dyas.a;
public class Solutiona {
public static void main(String[] args) {
Solutiona s1 = new Solutiona();
ListNode a1 = new ListNode(4);
ListNode a2 = new ListNode(1);
ListNode a3 = new ListNode(8);
ListNode a4 = new ListNode(4);
ListNode a5 = new ListNode(5);
// a2 = a1.next;Cannot read field "next" because "a2" is null
// a3 = a2.next;
// a4 = a3.next;
// a5 = a4.next;
a1.next = a2;
a2.next = a3;
a3.next = a4;
a4.next = a5;
ListNode cur = a1;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
ListNode b1 = new ListNode(5);
ListNode b2 = new ListNode(0);
ListNode b3=a3;
ListNode b4 = new ListNode(8);
ListNode b5 = new ListNode(4);
ListNode b6 = new ListNode(5);
b1.next = b2;
b2.next = b3;
b3.next = b4;
b4.next=b5;
b5.next=b6;
ListNode intersectionNode = s1.getIntersectionNode(a1, b1);
System.out.println("____"+intersectionNode.val);
// 验证是否找到了相交节点,并且该节点的值应该是 8
// System.out.println(res.val);
int gap = 5;
while (gap-- > 0) {
System.out.println("Gap is now: " + gap);
}
}
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) { // 求链表A的长度
lenA++;
curA = curA.next;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
//1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
//2. swap (curA, curB);
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap-- > 0) {
curA = curA.next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
}
给定一个链表的头节点 head
,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
。
如果链表中有某个节点,可以通过连续跟踪 next
指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos
来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos
是 -1
,则在该链表中没有环。注意:pos
不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
示例 1:
输入:head = [3,2,0,-4], pos = 1 输出:返回索引为 1 的链表节点 解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
输入:head = [1,2], pos = 0 输出:返回索引为 0 的链表节点
package days0605_60dyas.a;
public class Lian4 {
public static void main(String[] args) {
ListNode head = new ListNode(3);
ListNode node1 = new ListNode(2);
ListNode node2 = new ListNode(0);
ListNode node3 = new ListNode(4);
head.next = node1;
node1.next = node2;
node2.next = node3;
node3.next = node1; // 形成环
Lian4 lian4 = new Lian4();
ListNode result = lian4.detectCycle(head);
System.out.println(result.next.val);
}
public static ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null){
slow=slow.next;
fast=fast.next.next;
if (slow==fast){
ListNode index1=fast;
ListNode index2=slow;
while (index1!=index2){
index1=index1.next;
index2=index2.next;
}
return index1;
}
}
return null;
}
}