*
剑指 Offer 18. 删除链表的节点
题目描述:
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
示例 1:
输入: head = [4,5,1,9], val = 5 输出: [4,1,9] 解释: 给定你链表中值为 5
的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head == null) return null;
if(head.val == val){
return head.next;
}
ListNode cur = head;
while(cur.next.val != val){
cur = cur.next;
}
cur.next = cur.next.next;
return head;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val && head.next == null){
return null;
}
if(head.val == val && head.next != null){
head = head.next;
}
ListNode cur = head.next;
ListNode prev = head;
while(cur != null){
ListNode curNext = cur.next;
if(cur.val == val){
prev.next = cur.next;
return head;
}else{
prev = cur;
cur = curNext;
}
}
return head;
}
}
删除链表中的节点
面试题 02.03. 删除中间节点
题目描述:
若链表中的某个节点,既不是链表头节点,也不是链表尾节点,则称其为该链表的「中间节点」。
假定已知链表的某一个中间节点,请实现一种算法,将该节点从链表中删除。
例如,传入节点 c(位于单向链表 a->b->c->d->e->f 中),将其删除后,剩余链表为 a->b->d->e->f
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
移除链表元素
题目描述:
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6 输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1 输出:[]
示例 3:
输入:head = [7,7,7,7], val = 7 输出:[]
/**
* 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 removeElements(ListNode head, int val) {
if(head == null) return null;
ListNode prev = head;
ListNode cur = head.next;
while(cur != null){
if(cur.val == val){
prev.next = cur.next;
}else{
prev = cur;
}
cur = cur.next;
}
if(head.val == val){
return head.next;
}else{
return head;
}
}
}
剑指 Offer 24. 反转链表
剑指 Offer 24. 反转链表
题目描述:
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
示例:
输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return null;
ListNode prev =null;
ListNode cur = null;
while(head != null){
cur = head.next;
head.next = prev;
prev = head;
head = cur;
}
return prev;
}
}
链表的中间结点
给定一个头结点为 head 的非空单链表,返回链表的中间结点。
如果有两个中间结点,则返回第二个中间结点。
/**
* 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 middleNode(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next!= null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}
返回倒数第 k 个节点
面试题 02.02. 返回倒数第 k 个节点
题目描述:
实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。
注意:本题相对原题稍作改动
示例:
输入: 1->2->3->4->5 和 k = 2 输出: 4
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int kthToLast(ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
while(k-1 != 0){
fast = fast.next;
k--;
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
return slow.val;
}
}
合并两个排序的链表
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
示例1: 输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1 == null){
return l2;
}
if(l2 == null){
return l1;
}
ListNode list = new ListNode(0);
ListNode cur = list;
while(l1 != null && l2 != null){
if(l1.val <= l2.val){
cur.next = l1;
l1 = l1.next;
}else{
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
if(l1 != null){
cur.next = l1;
}else{
cur.next = l2;
}
//cur.next = (l1 != null) ? l1:l2;
return list.next;
}
}
链表分割
链表分割
题目描述:
现有一链表的头指针 ListNode*
pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
// write code here
if(pHead == null || pHead.next == null) return pHead;
ListNode h1 = new ListNode(0);
ListNode h2 = new ListNode(0);
ListNode cur1 = h1;
ListNode cur2 = h2;
while(pHead != null){
if(pHead.val < x){
cur1.next = new ListNode(pHead.val);
cur1 = cur1.next;
}else{
cur2.next = new ListNode(pHead.val);
cur2 = cur2.next;
}
pHead = pHead.next;
}
cur1.next = h2.next;
return h1.next;
}
}
删除链表中重复的结点
删除链表中重复的结点
题目描述:
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
示例1 输入 {1,2,3,3,4,4,5} 输出 {1,2,5}
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode deleteDuplication(ListNode pHead) {
if(pHead == null || pHead.next == null)return pHead;
ListNode head = new ListNode(0);
head.next = pHead;
ListNode prev = head;
ListNode cur = head.next;;
while(cur != null){
if(cur.next != null && cur.val == cur.next.val){
while(cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
prev.next = cur.next;
cur = cur.next;
}else{
prev = cur;
cur = cur.next;
}
}
return head.next;
}
}
链表的回文结构
链表的回文结构
题目描述:
对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。
给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。
测试样例: 1->2->2->1 返回:true
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
// write code here
Stack<Integer> stack = new Stack<>();
ListNode cur = A;
while(cur != null){
stack.push(cur.val);
cur = cur.next;
}
while(!stack.isEmpty()){
if(stack.pop() == A.val){
A = A.next;
}else{
return false;
}
}
return true;
}
}
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class PalindromeList {
public boolean chkPalindrome(ListNode A) {
// write code here
if(A == null) return false;
ListNode fast = A;
ListNode slow = A;
//找到链表中间节点
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
//反转链表后半部分
ListNode cur = slow.next;
while(cur != null){
ListNode curNext = cur.next;
cur.next = slow;
slow = cur;
cur = curNext;
}
//从头往中间 从后往中间依次比较是否相同
//只要A.val != slow.val 则返回false; 如果链表长度为偶数,则当A.next== slow时 说明相遇 即返回true
while(A != slow){
if(A.val != slow.val){
return false;
}
if(A.next == slow){//偶数情况
return true;
}
A = A.next;
slow = slow.next;
}
return true;
}
}
剑指 Offer 52. 两个链表的第一个公共节点
两个链表的第一个公共节点
题目描述:
/**
* 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 A = headA;
ListNode B = headB;
while(A != B){//若没有交集则A==B==null退出循环
A = (A != null ? A.next : headB);
B = (B != null ? B.next : headA);
}
return A;
}
}
/**
* 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;
}
int lenA = 0;
int lenB = 0;
ListNode pl = headA;
ListNode ps = headB;
while(pl != null){
pl = pl.next;
lenA++;
}
while(ps != null){
ps = ps.next;
lenB++;
}
pl = headA;
ps = headB;
int len = lenA - lenB;
if(len < 0) {
pl = headB;//pl指向较长的链表
ps = headA;//ps指向较短的链表
len = lenB - lenA;
}
while(len != 0){
pl = pl.next;
len--;
}
while(pl != ps){
pl = pl.next;
ps = ps.next;
}
return pl;
}
}
判断链表是否有环
环形链表
给定一个链表,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
如果链表中存在环,则返回 true 。 否则,返回 false 。
解法1:利用HashSet
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null) return false;
HashSet<ListNode> set = new HashSet<>();
ListNode cur = head;
while(cur != null) {
if(set.contains(cur)) {
return true;
}
set.add(cur);
cur = cur.next;
}
return false;
}
}
解法2:快慢指针
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null)return false;
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (slow == fast)
return true;
}
return false;
}
}
返回链表入环的第一个节点
环形链表 II
题目描述:
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
解法1:
/**
* 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 cur = head;
Set<ListNode> set = new HashSet<ListNode>();
while (cur != null) {
if (set.contains(cur)) {
return cur;
} else {
set.add(cur);
}
cur = cur.next;
}
return null;
}
}
解法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) {
if (head == null)return null;
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (slow == fast)
break;;
}
if(fast == null || fast.next == null){
return null;
}
slow = head;
while(fast != slow){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}