1.删除指定元素
/**
* 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) {
ListNode dummyHead = new ListNode(0);
dummyHead.next = head;
ListNode p = dummyHead;
while(p.next!=null){
if(p.next.val==val) p.next=p.next.next;
else p = p.next;
}
return dummyHead.next;
}
}
2.设计链表
class ListNode{
int val;
ListNode next;
public ListNode(int val){
this.val = val;
}
}
class MyLinkedList {
int size;
ListNode head;
public MyLinkedList() {
size = 0;
head = new ListNode(0);
}
public int get(int index) {
if(index<0||index>=size){
return -1;
}
ListNode cur = head;
for(int i=0;i<=index;i++) cur = cur.next;
return cur.val;
}
public void addAtHead(int val) {
addAtIndex(0,val);
}
public void addAtTail(int val) {
addAtIndex(size,val);
}
public void addAtIndex(int index, int val) {
if(index>size) return;
index = Math.max(0,index);
size++;
ListNode pred = head;
for(int i=0;i<index;i++) pred = pred.next;
ListNode toAdd = new ListNode(val);
toAdd.next = pred.next;
pred.next = toAdd;
}
public void deleteAtIndex(int index) {
if(index<0 || index>=size) return;
size--;
ListNode pred = head;
for(int i=0;i<index;i++) pred=pred.next;
pred.next = pred.next.next;
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/
3.反转链表
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pred = null,cur = head;
while(cur!=null){
ListNode next = cur.next;
cur.next = pred;
pred = cur;
cur = next;
}
return pred;
}
}
//递归
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next=null) return head;
//取出终极头结点
ListNode new_head = reverseList(head.next);
head.next.next = head;
//就是给原第一个节点用的
head.next = null;
//祖传头结点
return new_head;
}
}
4.两两交换
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode cur = dummy;
while(cur.next!=null&&cur.next.next!=null){
ListNode t = cur.next,t1 = cur.next.next.next;
cur.next = cur.next.next;
cur.next.next = t;
cur.next.next.next = t1;
cur = cur.next.next;
}
return dummy.next;
}
}
5.删除倒数N个元素
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0,head);
ListNode slow = dummy,fast = dummy.next;
for(int i=0;i<n;i++) fast = fast.next;
while(fast!=null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next;
return dummy.next;
}
}
6.链表相交
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p1 = headA,p2 = headB;
while(p1!=p2){
if(p1==null) p1 = headB;
else p1 = p1.next;
if(p2==null) p2 = headA;
else p2 = p2.next;
}
return p1;
}
}
7.链表有环&找到入口
public class Solution {
public ListNode detectCycle(ListNode head) {
if(head==null) return null;
ListNode slow = head,fast = head;
boolean hasCycle = false;
while(fast.next!=null&&fast.next.next!=null){
fast = fast.next.next;
slow = slow.next;
if(fast==slow){
hasCycle = true;
break;
}
}
if(hasCycle==true){
slow = head;
while(slow!=fast){
slow = slow.next;
fast = fast.next;
}
return fast;
}
return null;
}
}