代码随想录算法训练营第四天 | leetcode19、24、142、面试题02.07

24. Swap Nodes in Pairs

题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/

方法一

1 方法思想

双指针

2 代码实现

/**
 * 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) {
			if (head ==  null || head.next == null){
			return head;
		}
		ListNode sentry = new ListNode(0, head);
		ListNode pre = sentry;
		ListNode cur = head;
		ListNode last = head.next;
		
		ListNode temp = new ListNode(0);
		
		while (cur != null && last != null){
			// 交换节点
			cur.next = last.next;
			last.next = cur;
			pre.next  = last;
			
			// 指针移动
			if (cur.next != null){
				pre = cur;
				cur = cur.next;
				last = cur.next;
			}else {
				break;
			}
		}
		
		return sentry.next;
	}
}

3 复杂度分析

时间复杂度:O(n)
空间复杂度:O(1)

19. Remove Nth Node From End of List

题目链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/

方法一 双指针

1 方法思想

2 代码实现

/**
 * 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 pre = new ListNode(0, head);
		ListNode cur = pre;
		ListNode sign = head;
		while (n > 1) {
			sign = sign.next;
			n--;
		}
		while (sign.next  != null){
			pre = pre.next;
			sign = sign.next;
		}
		pre.next = pre.next.next;
		return cur.next;
		
		
	}
	
}

3 复杂度分析

时间复杂度:O(n)
空间复杂度:O(1)

面试题 02.07. Intersection of Two Linked Lists LCCI

题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/## 方法一

1 方法思想

如果有节点交叉,从交叉节点往后,两个链表的长度肯定是相同的。

2 代码实现

/**
 * 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) {
        
		ListNode curA = headA;
		ListNode curB = headB;
		int lenA = 0;
		int lenB = 0;
		while (curA != null) {
			lenA++;
            curA = curA.next;
		}
		while (curB != null) {
			lenB++;
            curB = curB.next;
		}
		while (lenA > lenB){
			lenA --;
			headA = headA.next;
		}
		while (lenB > lenA){
			lenB --;
			headB = headB.next;
		}
		while (headA != null){
			if (headA == headB) {
				return headA;
			}else {
				headA = headA.next;
				headB = headB.next;
			}
		}
		return null;
    }
}

3 复杂度分析

时间复杂度:O(m + n)
空间复杂度:O(1)

142. Linked List Cycle II

题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/

方法一 快慢指针

1 方法思想

使用快慢指针遍历链表,如果有相遇,则证明有环。2*(x + y) = x + y + n*(y + z) -> x = (n - 1)* (y + z) + z
如果有环,从相遇节点开始遍历,同时有另一指针从头节点遍历,相遇时的节点就是入口节点。
在这里插入图片描述

2 代码实现

/**
 * 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) {
        		
		//2 * (x + y) = x + y + N * (y + z);
		//x + y = N * (y + z);
		//x = (N - 1) * (y + z) + z;
		//N = 1时; x = z;
		//N > 1时; ((N - 1) * (y + z) + z) % (y + z) = z;
		if (head == null || head.next == null) return null;
		ListNode fast = head;
		ListNode slow  = fast;
		while (fast != null && fast.next != null){
			
			fast = fast.next.next;
			slow = slow.next;
			
			if (fast == slow){ // 检测到有环
				ListNode l1 = head;
				ListNode l2 = fast;
				while (l1 != l2){
					l1 = l1.next;
					l2 = l2.next;
				}
				return l1;
			}
		}
		return null;
    }
}

3 复杂度分析

时间复杂度:O(N)
空间复杂度:O(1)

4 涉及到知识点

学习链接

https://leetcode.cn/problems/linked-list-cycle-ii/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值