力扣_链表
删除链表中的节点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
//删除node节点
//先将node节点的值编程node节点后面的值
//再将node节点的指向由其下一个变成它的下下一个
//总之就是将node节点的下一个元素的值和指向变成node的
class Solution {
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}
}
删除链表的倒数第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; }
* }
*/
//删除倒数第n个节点,首先要判断n与链表的长度大小
//如果n比链表长度大则返回null
//如果n比链表长度小,则删除的是正数(链表长度-n+1)个节点
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode before = new ListNode(0,head);
//求链表的长度
int length = Length(before);
ListNode after = before;
for(int i = 1;i < length-n+1;i++){
after = after.next;
}
after.next = after.next.next;
return before.next;
}
//求链表的长度
public int Length(ListNode node){
int length = 0;
node = node.next;
while(node != null){
node = node.next;
length++;
}
return length;
}
}
反转链表
/**
* 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 reverseList(ListNode head) {
ListNode rev = null;
ListNode node = head;
while(node != null){
ListNode next = node.next;
node.next = rev;
rev = node;
node = next;
}
return rev;
}
}
合并两个有序链表
/**
* 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; }
* }
*/
//设置一个指针用于最后返回头节点用
//设置另外一个指针用于保存过程中的值
//保存过程中一直比较list1和list2节点值的大小,小的保存下来,并且指针向后移动
//最后将未保存下来的数据保存下来
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
//设置哨兵节点
ListNode ln1 = new ListNode(-1);
//过程保存值节点
ListNode ln2 = ln1;
while(list1 != null && list2 != null){
if(list1.val <= list2.val){
ln2.next = list1;
list1 = list1.next;
}else{
ln2.next = list2;
list2 = list2.next;
}
ln2 = ln2.next;
}
if(list1 == null){
ln2.next = list2;
}else{
ln2.next = list1;
}
return ln1.next;
}
}
回文链表
/**
* 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; }
* }
*/
//先用一个List保存链表中的元素
//然后设置一个头结点和末尾节点用用于判断该链表是不是对称的
//直到到达中间为止
class Solution {
public boolean isPalindrome(ListNode head) {
//设置List保存链表中的元素
List<Integer> list = new ArrayList<Integer>();
while(head != null){
list.add(head.val);
head = head.next;
}
//设置头节点和末尾节点
int start = 0;
int end = list.size()-1;
while(start < end){
if(list.get(start) != list.get(end)){
return false;
}
start++;
end--;
}
return true;
}
}
环形链表
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
//这题可以用set集合
//因为set集合不能存相同的值
public class Solution {
public boolean hasCycle(ListNode head) {
//设置set用于存节点
Set<ListNode> set = new HashSet<ListNode>();
//顺序存入链表节点,如果中间遇到重复的直接返回true,代表是循环链表
while(head != null){
//存入返回true,否则返回false
if(!set.add(head)){
return true;
}
head = head.next;
}
return false;
}
}

浙公网安备 33010602011771号