leetcode21 合并两个有序链表
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
/**
* 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; }
* }
*/
// v1.0 循环
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummyNode = new ListNode(0);
ListNode curr = dummyNode;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
curr.next = l1;
curr = curr.next;
l1 = l1.next;
}else{
curr.next = l2;
curr = curr.next;
l2 = l2.next;
}
}
if(l1 == null){
curr.next = l2;
}
if(l2 == null){
curr.next = l1;
}
return dummyNode.next;
}
}
// v2.0 递归
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null) return l2;
if(l2==null) return l1;
ListNode res = l1.val<l2.val? l1:l2;
res.next = mergeTwoLists(res.next, l1.val >= l2.val? l1:l2);
return res;
}
}
leetcode 203 移除链表元素
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回 新的头节点 。
/**
* 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; }
* }
*/
//v1.0 添加一个虚拟头节点
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode curr = dummyNode;
while(curr.next!=null){
if(curr.next.val == val){
curr.next = curr.next.next;
}else{
curr = curr.next;
}
}
return dummyNode.next;
}
}
//v2.0 递归 ★(还未看懂)
class Solution {
public ListNode removeElements(ListNode head, int val) {
if(head==null)
return null;
head.next=removeElements(head.next,val);
if(head.val==val){
return head.next;
}else{
return head;
}
}
}
leetcode 206 反转链表
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
/*
方法1:迭代 时间复杂度O(n) 空间复杂度O(1)
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while(curr!=null){
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
}
leetcode148 排序链表
/**
* 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 ListNode sortList(ListNode head) {
// 归并排序
return mergeSort(head);
}
public ListNode mergeSort(ListNode head){
// 如果结点为空或只有一个结点的话,没必要排序
if(head==null||head.next==null){
return head;
}
ListNode dummy = new ListNode(-1,head);
ListNode quick = head, slow = head;
while(quick!=null&&quick.next!=null){
dummy = dummy.next;
quick = quick.next.next;
slow = slow.next;
}
dummy.next = null;
ListNode left = mergeSort(head);
ListNode right = mergeSort(slow);
return merge(left,right);
}
public ListNode merge(ListNode left, ListNode right){
ListNode dummy = new ListNode(-1);
ListNode tmp = dummy;
while(left!=null&&right!=null){
if(left.val<right.val){
tmp.next = left;
left = left.next;
}else{
tmp.next = right;
right = right.next;
}
tmp = tmp.next;
}
if(left!=null){
tmp.next = left;
}
if(right!=null){
tmp.next = right;
}
return dummy.next;
}
leetcode234 回文链表
/**
* 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 boolean isPalindrome(ListNode head) {
//定义快慢指针
ListNode slow = head;
ListNode fast = head;
while(fast!=null&&fast.next!=null){
fast = fast.next.next;
slow = slow.next;
}
ListNode curr = slow;
if(fast!=null){
curr = slow.next; //curr用来反转后半链表,若为奇数链表,则后移一位
}
//反转链表
ListNode prev = null;
while(curr!=null){
ListNode temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
//判断是否是回文链表
while(prev!=null){
if(prev.val==head.val){
prev = prev.next;
head = head.next;
}
else{
return false;
}
}
return true;
}
}
leetcode 160 相交链表
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null || headB==null){
return null;
}
ListNode leftNode = headA;
ListNode rightNode = headB;
while(leftNode!=rightNode){
leftNode = leftNode==null? headB:leftNode.next;
rightNode = rightNode==null? headA:rightNode.next;
}
return leftNode;
}
}
leetcode 82 删除排序链表中的重复元素II
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode nextNode = head.next;
if(head.val==nextNode.val){
while(nextNode!=null&&head.val==nextNode.val){
nextNode = nextNode.next;
}
head = deleteDuplicates(nextNode);
}else{
head.next = deleteDuplicates(nextNode);
}
return head;
}
}
leetcode 2 两数相加
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyOne = new ListNode(-1, l1);
ListNode dummyTwo = new ListNode(-1, l2);
ListNode dummy = new ListNode(-1);
ListNode curr = dummy;
ListNode currOne = dummyOne;
ListNode currTwo = dummyTwo;
int carry = 0;
int val = 0;
while(currOne.next!=null||currTwo.next!=null){
val = (currOne.next.val+currTwo.next.val+carry)%10;
carry = (currOne.next.val+currTwo.next.val+carry)/10;
curr.next = new ListNode(val);
curr = curr.next;
currOne = currOne.next;
currTwo = currTwo.next;
if(currOne.next==null&&currTwo.next==null){
break;
}
if(currOne.next==null){
currOne.next = new ListNode(0);
}
if(currTwo.next==null){
currTwo.next = new ListNode(0);
}
}
if(carry>0){
curr.next = new ListNode(carry);
}
return dummy.next;
}
}
leetcode 19 删除链表倒数第N个结点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
// 构建虚拟节点,因为可能删除首结点
ListNode dummy = new ListNode(-1,head);
ListNode low = dummy;
ListNode quick = dummy;
int count = 0;
if(head.next==null){
return null;
}
while(count!=n+1){
quick = quick.next;
count++;
}
while(quick!=null){
quick = quick.next;
low = low.next;
}
low.next = low.next.next;
return dummy.next;
}
}
leetcode 23 合并K个升序链表
//v1.0 暴力合并k个链表
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode dummy = new ListNode(-1);
ListNode tmp = dummy;
dfs(lists,tmp);
return dummy.next;
}
public void dfs(ListNode[] lists, ListNode node){
int index = -1;
int max = 100000;
for(int i=0;i<lists.length;i++){
if(lists[i]!=null){
if(lists[i].val<max){
index = i;
max = lists[i].val;
}
}
}
if(index==-1){
return;
}else{
node.next = lists[index];
lists[index] = lists[index].next;
node = node.next;
dfs(lists, node);
}
}
}
// v2.0 分治算法
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
return merge(lists, 0, lists.length-1);
}
public ListNode merge(ListNode[] lists, int l, int r){
if(l==r){
return lists[l];
}
if(l>r){
return null;
}
int mid = (l+r)>>1;
return mergeTwoLists(merge(lists,l,mid), merge(lists,mid+1,r));
}
public ListNode mergeTwoLists(ListNode a, ListNode b){
ListNode dummy = new ListNode(-1);
ListNode tmp = dummy;
while(a!=null&&b!=null){
if(a.val>b.val){
tmp.next = b;
b = b.next;
}else{
tmp.next = a;
a = a.next;
}
tmp = tmp.next;
}
if(a!=null){
tmp.next = a;
}else{
tmp.next = b;
}
return dummy.next;
}
}
leetcode 142 环形链表
/**
* 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 qucik = head;
ListNode slow = head;
boolean hasCycle = false;
while(qucik.next!=null&&qucik.next.next!=null){
qucik=qucik.next.next;
slow = slow.next;
if(qucik == slow){
hasCycle = true;
break;
}
}
if(hasCycle){
ListNode tmp = head;
while(tmp!=slow){
tmp = tmp.next;
slow = slow.next;
}
return tmp;
}
return null;
}
}