剑指offer18 删除链表中的节点
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
注意:此题对比原题有改动
示例 1:
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:
输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 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 head;
}
ListNode a = new ListNode(0);
ListNode b = a;
while(head!=null){
if(head.val != val){
ListNode ans = new ListNode(head.val);
a.next = ans;
a = a.next;
}
head = head.next;
}
return b.next;
}
}
面试题0…06 回文链表
编写一个函数,检查输入的链表是否是回文的。
示例 1:
输入: 1->2
输出: false
示例 2:
输入: 1->2->2->1
输出: true
思路:将原链表的值全部送入栈中(倒置链表值)
将链表中的值与栈中的值依次送出,比较是否相同(比较len/2次即可)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isPalindrome(ListNode head) {
Stack<Integer> stack = new Stack<Integer>();
ListNode a = new ListNode(0,head);
a = a.next;
int num = 0;
while(a!=null){
stack.push(a.val);
a = a.next;
num++;
}
for(int i = 0;i<num/2;i++){
int ans1 = stack.pop();
int ans2 = head.val;
head = head.next;
if(ans1 != ans2){
return false;
}
}
return true;
}
}
力扣1290 二进制链表转整数
class Solution {
public int getDecimalValue(ListNode head) {
int len = getLength(head);
int sum = 0;
for(int i=0;i<len;i++){
sum += head.val * Math.pow(2,len-1-i);
head = head.next;
}
return sum;
}
public int getLength(ListNode head){
int len = 0;
while(head!=null){
head = head.next;
len++;
}
return len;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int getDecimalValue(ListNode head) {
int sum = 0;
while (head != null) {
sum = (sum << 1) + head.val;
head = head.next;
}
return sum;
}
}
剑指offer25合并两个排序的链表
/**
* 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) {
int len1 = getLength(l1);
int len2 = getLength(l2);
if(len1 == 0){
return l2;
}
if(len2 == 0){
return l1;
}
int[] arr = new int[len1+len2];
for(int i=0; i<len1; i++){
arr[i] = l1.val;
l1 = l1.next;
}
for(int i=len1; i<len1+len2; i++){
arr[i] = l2.val;
l2 = l2.next;
}
Arrays.sort(arr);
ListNode head = new ListNode(arr[0]);
ListNode cur = head;
for(int i=1; i<len1+len2; i++){
ListNode ans = new ListNode(arr[i]);
head.next = ans;
head = head.next;
}
return cur;
}
public int getLength(ListNode head){
int len = 0;
while(head!=null){
len++;
head = head.next;
}
return len;
}
}
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dum = new ListNode(0), cur = dum;
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;
}
cur.next = l1 != null ? l1 : l2;
return dum.next;
}
}
力扣328 奇偶链表
有问题 需要开辟新空间的方法,
即链表的空间复杂度为O(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; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
ListNode a = new ListNode(0);
ListNode cur1 = a;
ListNode b = new ListNode(0);
ListNode cur2 = b;
int i = 1;
while(head!=null){
if(i%2==1){
ListNode ans1 = new ListNode(head.val);
a.next = ans1;
a = a.next;
head = head.next;
}
else{
ListNode ans2 = new ListNode(head.val);
b.next = ans2;
b = b.next;
head = head.next;
}
i++;
}
a.next = cur2.next;
return cur1.next;
}
}
力扣23 合并K个升序链表
朴素法:
遍历链表数组,每次将两个链表合并排序,返回新的链表
合并两个有序链表参考剑指offer25
/**
* 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 mergeKLists(ListNode[] lists) {
ListNode ans = null;
for (int i = 0; i < lists.length; ++i) {
ans = mergeTwoLists(ans, lists[i]);
}
return ans;
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
int len1 = getLength(l1);
int len2 = getLength(l2);
if(len1 == 0){
return l2;
}
if(len2 == 0){
return l1;
}
int[] arr = new int[len1+len2];
for(int i=0; i<len1; i++){
arr[i] = l1.val;
l1 = l1.next;
}
for(int i=len1; i<len1+len2; i++){
arr[i] = l2.val;
l2 = l2.next;
}
Arrays.sort(arr);
ListNode head = new ListNode(arr[0]);
ListNode cur = head;
for(int i=1; i<len1+len2; i++){
ListNode ans = new ListNode(arr[i]);
head.next = ans;
head = head.next;
}
return cur;
}
public int getLength(ListNode head){
int len = 0;
while(head!=null){
len++;
head = head.next;
}
return len;
}
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
ListNode ans = null;
for (int i = 0; i < lists.length; ++i) {
ans = mergeTwoLists(ans, lists[i]);
}
return ans;
}
public ListNode mergeTwoLists(ListNode a, ListNode b) {
if (a == null || b == null) {
return a != null ? a : b;
}
ListNode head = new ListNode(0);
ListNode tail = head, aPtr = a, bPtr = b;
while (aPtr != null && bPtr != null) {
if (aPtr.val < bPtr.val) {
tail.next = aPtr;
aPtr = aPtr.next;
} else {
tail.next = bPtr;
bPtr = bPtr.next;
}
tail = tail.next;
}
tail.next = (aPtr != null ? aPtr : bPtr);
return head.next;
}
}