链表相关面试题
链表中的节点每k个一组翻转
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
public ListNode reverseKGroup (ListNode head, int k) {
// write code here
if(head == null || head.next == null || k < 2) {
return head;
}
ListNode newHead = new ListNode(-1);
newHead.next = head;
ListNode prev = newHead;
ListNode cur = head;
ListNode tmp = null;
int len = 0;
while(cur != null) {
len++;
cur = cur.next;
}
cur = head;
//头插法反转链表
for(int i = 0; i < len / k; i++) {
for(int j = 1; j < k; j++) {
tmp = cur.next;
cur.next = tmp.next;
tmp.next = prev.next;
prev.next = tmp;
}
prev = cur;
cur = prev.next;
}
return newHead.next;
}
}
两数相加
https://leetcode-cn.com/problems/add-two-numbers/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null) {
return l1;
}
if(l2 == null) {
return l1;
}
ListNode head = null, tail = null;
int carry = 0;
while(l1 != null || l2 != null) {
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
if(head == null) {
head = tail = new ListNode(sum % 10);
}else {
tail.next = new ListNode(sum % 10);
tail = tail.next;
}
carry = sum / 10;
if(l1 != null) {
l1 = l1.next;
}
if(l2 != null) {
l2 = l2.next;
}
}
if(carry > 0) {
tail.next = new ListNode(carry);
}
return head;
}
}
链表求和(两个链表生成相加链表)正序排列
题目连接:https://leetcode-cn.com/problems/sum-lists-lcci/
利用逆序的方法来解决
import java.util.*;
public class Solution {
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
if(head1 == null) {
return head2;
}
if(head2 == null) {
return head1;
}
ListNode l1 = reverse(head1);
ListNode l2 = reverse(head2);
int carry = 0;
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while(l1 != null || l2 != null) {
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
carry = sum / 10;
ListNode node = new ListNode(sum % 10);
tmp.next = node;
tmp = tmp.next;
if(l1 != null) {
l1 = l1.next;
}
if(l2 != null) {
l2 = l2.next;
}
}
if(carry > 0) {
tmp.next = new ListNode(1);
}
ListNode ret = reverse(newHead.next);
return ret;
}
public ListNode reverse(ListNode head){
if(head == null) {
return null;
}
ListNode prev = null;
ListNode cur = head;
ListNode curNext = null;
while(cur != null) {
curNext = cur.next;
cur.next = prev;
prev = cur;
cur = curNext;
}
return prev;
}
}
利用栈来解决
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head1 ListNode类
* @param head2 ListNode类
* @return ListNode类
*/
public ListNode addInList (ListNode head1, ListNode head2) {
// write code here
Stack<Integer> stack1 = buildStack(head1);
Stack<Integer> stack2 = buildStack(head2);
ListNode newHead = new ListNode(-1);
int carry = 0;
while(!stack1.isEmpty() || !stack2.isEmpty() || carry != 0) {
int n1 = stack1.isEmpty() ? 0 : stack1.pop();
int n2 = stack2.isEmpty() ? 0 : stack2.pop();
//头插法
int sum = n1 + n2 + carry;
carry = sum / 10;
ListNode node = new ListNode(sum % 10);
node.next = newHead.next;
newHead.next = node;
}
return newHead.next;
}
public Stack<Integer> buildStack(ListNode head){
Stack<Integer> stack = new Stack<Integer>();
if(head == null) {
return stack;
}
while(head != null){
stack.push(head.val);
head = head.next;
}
return stack;
}
}
链表求和(逆序排列)
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null) {
return l2;
}
if(l2 == null) {
return l1;
}
int carry = 0;
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while(l1 != null || l2 != null) {
int n1 = l1 != null ? l1.val : 0;
int n2 = l2 != null ? l2.val : 0;
int sum = n1 + n2 + carry;
carry = sum / 10;
ListNode node = new ListNode(sum % 10);
tmp.next = node;
tmp = tmp.next;
if(l1 != null) {
l1 = l1.next;
}
if(l2 != null) {
l2 = l2.next;
}
}
if(carry > 0) {
tmp.next = new ListNode(1);
}
return newHead.next;
}
}
相交链表
题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
//暴力解法
class Solution {
public int getL(ListNode cur)
{
if(cur==null) return 0;
int len=0;
while(cur!=null)
{
cur=cur.next;
len++;
}
return len;
}
public ListNode findFirstCommonNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null) return null;
int lenA=getL(headA);
int lenB=getL(headB);
if(lenA>lenB)
{
while(lenA-lenB>0)
{
headA=headA.next;
lenA--;
}
}else if(lenA<lenB){
while(lenB-lenA>0)
{
headB=headB.next;
lenB--;
}
}
while(headA!=headB)
{
headA=headA.next;
headB=headB.next;
}
return headA;
}
}
//双指针
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p=headA;
ListNode q=headB;
while(p!=q)
{
if(p!=null)
{
p=p.next;
}else{
p=headB;
}
if(q!=null)
{
q=q.next;
}else{
q=headA;
}
}
return p;
}
}
反转链表
题目连接:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
-
反转链表1
-
题目描述:反转一个单链表。
-
分析:利用一个节点的前驱,后继与本节点相互转换;简单来说就是前边后,后变前。大家都懂得
class Solution { public ListNode reverseList(ListNode head) { if(head==null) return null; ListNode prev=null,cur=head,curNext=null; while(cur!=null) { curNext=cur.next; cur.next=prev; prev=cur; cur=curNext; } return prev; } }
反转链表2(指定区间内的反转)
https://leetcode-cn.com/problems/reverse-linked-list-ii/
题目描述:给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。
class Solution {
public ListNode reverseBetween(ListNode head, int l, int r) {
if(head == null) {
return null;
}
ListNode newHead = new ListNode(-1);
newHead.next = head;
ListNode tmp = newHead;
for(int i = 0; i < l - 1; i++){
tmp = tmp.next;
}
ListNode prev = tmp.next;
ListNode cur = prev.next;
ListNode curNext = null;
for(int i = 0; i < r - l; i++) {
curNext = cur.next;
cur.next = prev;
prev = cur;
cur = curNext;
}
tmp.next.next = cur;
tmp.next = prev;
return newHead.next;
}
}
链表中环的入口节点
public class Solution {
public ListNode EntryNodeOfLoop(ListNode head) {
if(head == null) {
return null;
}
ListNode fast = head;
ListNode slow = 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;
}
while(slow != head) {
slow = slow.next;
head = head.next;
}
return head;
}
}
合并两个有序链表
题目连接:https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/
/**
* 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 newHead = new ListNode(-1);
ListNode tmp = newHead;
while(l1 != null && l2 != null) {
if(l1.val <= l2.val) {
tmp.next = l1;
l1 = l1.next;
}else {
tmp.next = l2;
l2 = l2.next;
}
tmp = tmp.next;
}
if(l1 != null) {
tmp.next = l1;
}
if(l2 != null) {
tmp.next = l2;
}
return newHead.next;
}
}
两个链表的第一个公共节点
public class Solution {
public ListNode FindFirstCommonNode(ListNode h1, ListNode h2) {
if(h1 == null || h2 == null) {
return null;
}
ListNode p = h1, q = h2;
while(p != q) {
if(p != null) {
p = p.next;
}else {
p = h2;
}
if(q != null) {
q = q.next;
}else {
q = h1;
}
}
return p;
}
}
从有序链表中删除重复节点
-
从有序链表中删除重复节点Ⅰ
-
题目描述:给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
// write code here
if(head == null) return null;
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
ListNode cur = head;
while(cur != null)
{
if(cur.next != null && cur.val == cur.next.val)
{
while(cur.next != null && cur.val == cur.next.val)
{
cur = cur.next;
}
cur = cur.next;
}else{
tmp.next=cur;
tmp = tmp.next;
cur = cur.next;
}
}
tmp.next = null;
return newHead.next;
}
}
-
从有序链表中删除重复节点Ⅱ
-
题目描述:给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
-
分析:分情况:重复节点与不重复节点;找到不重复节点连接在新链表的尾部
-
public class Solution {
public ListNode deleteDuplicates (ListNode head) {
// write code here
if(head == null) return null;
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
ListNode cur = head;
while(cur != null)
{
if(cur.next != null && cur.val == cur.next.val)
{
while(cur.next != null && cur.val == cur.next.val)
{
cur = cur.next;
}
}else{
tmp.next=cur;
tmp = tmp.next;
cur = cur.next;
}
}
tmp.next = null;
return newHead.next;
}
}
删除链表的节点
题目链接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
-
分析:双指针
class Solution { public ListNode deleteNode(ListNode head, int val) { if(head==null) return null; if(head.val==val) return head.next; ListNode prev=head; ListNode cur=head.next; while(cur!=null){ if(cur.val==val){ prev.next=cur.next; prev=cur; }else{ prev=cur; } cur=cur.next; } return head; } }
//单指针
public ListNode deleteNode (ListNode head, int val) {
// write code here
if(head == null) {
return null;
}
if(head.val == val) {
return head.next;
}
ListNode cur = head;
while(cur != null) {
if(cur.next.val == val) {
cur.next = cur.next.next;
break;
}
cur = cur.next;
}
return head;
}
}
链表中倒数第n个节点
https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
if(head == null) {
return null;
}
ListNode fast = head;
ListNode slow = head;
for(int i = 0; i < k; i++) {
if(fast == null) {
return null;
}else {
fast = fast.next;
}
}
while(fast != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
删除链表的倒数第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; }
* }
*/
public class Solution {
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
if(head == null){
return null;
}
ListNode fast = head;
ListNode slow = head;
for(int i = 0; i < n; i++) {
if(fast == null) {
return null;
}else {
fast = fast.next;
}
}
if(fast == null) {
return head.next;
}
ListNode prev = null;
while(fast != null) {
prev = slow;
slow = slow.next;
fast = fast.next;
}
prev.next = slow.next;
return head;
}
}
交换链表中的相邻节点
题目连接:https://leetcode-cn.com/problems/swap-nodes-in-pairs/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode newList = new ListNode(-1);
newList.next = head;
ListNode prev = newList;
while(prev.next != null && prev.next.next != null){
ListNode l1 = prev.next;
ListNode l2 = prev.next.next;
l1.next = l2.next;
l2.next = l1;
prev.next = l2;
prev = l1;
}
return newList.next;
}
}
回文链表
题目连接:https://leetcode-cn.com/problems/palindrome-linked-list/
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null) {
return true;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode prev = null;
ListNode slowNext = null;
while(slow != null) {
slowNext = slow.next;
slow.next = prev;
prev = slow;
slow = slowNext;
}
while(prev != null) {
if(prev.val != head.val) {
return false;
}
prev = prev.next;
head = head.next;
}
return true;
}
}
分隔链表
题目连接:https://leetcode-cn.com/problems/split-linked-list-in-parts/
class Solution {
public ListNode[] splitListToParts(ListNode root, int k) {
ListNode[] res = new ListNode[k];
if(null == root) return res;
//链表长度
int count= 0;
ListNode cur = root;
while(null != cur){
count++;
cur = cur.next;
}
cur = root;
//链表长度小于等于分组,也就是每组一个节点还有的组没有节点
if(k >= count){
for(int i =0;i < count;i++){
res[i] = cur;
ListNode temp = cur.next;
cur.next = null;
cur = temp;
}
}else{
int preCount = count/k;
int remain = count%k;
int[] numArr = new int[k];
for(int i = 0;i < k;i++){
numArr[i] = remain-- >0?preCount+1:preCount;
}
for(int i=0;i < k;i++){
int num = numArr[i];
res[i] = cur;
while(--num > 0){
cur = cur.next;
}
ListNode temp = cur.next;
cur.next = null;
cur = temp;
}
}
return res;
}
}
链表元素按奇偶聚集
题目连接:https://leetcode-cn.com/problems/odd-even-linked-list/
class Solution {
public ListNode oddEvenList(ListNode head) {
if(null == head){
return null;
}
ListNode odd = head;
ListNode oddTail = odd;
ListNode even = head.next;
ListNode evenTail = even;
while(evenTail != null && evenTail.next != null){
oddTail.next = oddTail.next.next;
evenTail.next = evenTail.next.next;
oddTail = oddTail.next;
evenTail = evenTail.next;
}
oddTail.next = even;
return head;
}
}
复杂链表的复刻
题目连接:https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/
//HashMap
class Solution {
public ListNode copyRandomList(ListNode head) {
if(head==null) return null;
HashMap<ListNode,ListNode> map=new HashMap<>();
ListNode cur=head;
while(cur!=null)
{
ListNode node=new ListNode(cur.val);
map.put(cur,node);
cur=cur.next;
}
cur=head;
while(cur!=null)
{
map.get(cur).next=map.get(cur.next);
map.get(cur).random=map.get(cur.random);
cur=cur.next;
}
return map.get(head);
}
}
//复制连接法
class Solution {
public ListNode copyRandomList(ListNode head) {
if(head==null) return null;
ListNode cur=head;
while(cur!=null)
{
ListNode node=new ListNode(cur.val);
node.next=cur.next;
cur.next=node;
cur=node.next;
}
cur=head;
while(cur!=null)
{
if(cur.random!=null)
{
cur.next.random=cur.random.next;
}
cur=cur.next.next;
}
cur=head;
ListNode newHead=new ListNode(-1);
ListNode tmp=newHead;
while(cur!=null)
{
tmp.next=cur.next;
tmp=tmp.next;
cur.next=tmp.next;
cur=cur.next;
}
return newHead.next;
}
}
排序链表(链表排序)
题目连接:https://leetcode-cn.com/problems/sort-list/
归并思想
class Solution {
public ListNode sortList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
ListNode fast = head;
ListNode slow = head;
ListNode prev = null;
while(fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = null;
ListNode l = sortList(head);
ListNode r = sortList(slow);
return merge(l, r);
}
public ListNode merge(ListNode l1, ListNode l2) {
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while(l1 != null && l2 != null) {
if(l1.val <= l2.val) {
tmp.next = l1;
l1 = l1.next;
}else {
tmp.next = l2;
l2 = l2.next;
}
tmp = tmp.next;
}
if(l1 != null) {
tmp.next = l1;
}
if(l2 != null) {
tmp.next = l2;
}
return newHead.next;
}
}
重排链表
public class Solution {
public void reorderList(ListNode head) {
if(head == null) {
return ;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode slowNext = slow.next;
slow.next = null;
ListNode newHead = reverse(slowNext);
ListNode cur = head;
ListNode next = null;
int k = 0;
while(newHead != null && cur != null) {
if(k % 2 == 0) {
next = cur.next;
cur.next = newHead;
cur = next;
}else {
next = newHead.next;
newHead.next = cur;
newHead = next;
}
k++;
}
}
public ListNode reverse(ListNode head) {
if(head == null) {
return null;
}
ListNode prev = null;
ListNode cur = head;
ListNode curNext = null;
while(cur != null) {
curNext = cur.next;
cur.next = prev;
prev = cur;
cur = curNext;
}
return prev;
}
}
单链表的排序
public class Solution {
public ListNode sortInList (ListNode head) {
// write code here
if(head == null || head.next == null) {
return head;
}
ListNode fast = head;
ListNode slow = head;
ListNode prev = null;
while(fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
prev.next = null;
ListNode l = sortInList(head);
ListNode r = sortInList(slow);
return merge(l, r);
}
public ListNode merge(ListNode l1, ListNode l2) {
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while(l1 != null && l2 != null) {
if(l1.val <= l2.val) {
tmp.next = l1;
l1 = l1.next;
}else {
tmp.next = l2;
l2 = l2.next;
}
tmp = tmp.next;
}
if(l1 != null) {
tmp.next = l1;
}
if(l2 != null) {
tmp.next = l2;
}
return newHead.next;
}
}
合并k个已排序的链表
import java.util.*;
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
if (lists == null) {
return null;
}
PriorityQueue<ListNode> queue = new PriorityQueue<>((ListNode a, ListNode b) -> (a.val - b.val));
for (ListNode list : lists) {
if (list != null) {
queue.add(list);
}
}
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while (!queue.isEmpty()) {
ListNode node = queue.poll();
tmp.next = node;
if (node.next != null) {
queue.add(node.next);
}
tmp = tmp.next;
}
return newHead.next;
}
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if(lists == null || lists.length ==0) {
return null;
}
return func(lists, 0, lists.length - 1);
}
private ListNode func(ListNode[] lists, int left, int right) {
if(left == right){
return lists[left];
}
int mid = left + right >> 1;
ListNode leftList = func(lists, left, mid);
ListNode rightList = func(lists, mid + 1, right);
return merge(leftList, rightList);
}
public ListNode merge(ListNode first,ListNode second) {
ListNode newHead = new ListNode(-1);
ListNode tmp = newHead;
while (first != null && second != null) {
if(first.val <= second.val){
tmp.next = first;
first = first.next;
}
else{
tmp.next = second;
second = second.next;
}
tmp = tmp.next;
}
if(first != null){
tmp.next = first;
}
if(second != null){
tmp.next = second;
}
return newHead.next;
}
}
持续更新中。。。。。。。