1.设计链表力扣707-单链表
class ListNode {
int val;
ListNode next;
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 pre = head;
for(int i = 0;i < index; i ++){
pre = pre.next;
}
ListNode newnode = new ListNode(val);
newnode.next = pre.next;
pre.next = newnode;
}
public void deleteAtIndex(int index) {
if(index < 0 || index >= size) return;
size --;
if(index == 0){
head = head.next;
return;
}
ListNode pre = head;
for(int i = 0; i < index; i ++){
pre = pre.next;
}
pre.next = pre.next.next;
}
}
2.设计链表-力扣707双向链表
class ListNode {
int val;
ListNode prev, next;
ListNode(int val) {
this.val = val;
}
}
class MyLinkedList {
int size;
ListNode head, tail;
public MyLinkedList() {
this.size = 0;
this.head = new ListNode(0);
this.tail = new ListNode(0);
head.next = tail;
tail.prev = head;
}
public int get(int index) {
if (index < 0 || index >= size)
return -1;
ListNode cur = head;
if (index >= size / 2) {
cur = tail;
for (int i = 0; i < size - index; i++) {
cur = cur.prev;
}
}else {
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 pre = head;
for (int i = 0; i < index; i++) {
pre = pre.next;
}
ListNode newNode = new ListNode(val);
newNode.next = pre.next;
pre.next.prev = newNode;
newNode.prev = pre;
pre.next = newNode;
}
public void deleteAtIndex(int index) {
if (index < 0 || index >= size)
return;
size--;
ListNode pre = head;
for (int i = 0; i < index; i++) {
pre = pre.next;
}
pre.next.next.prev = pre;
pre.next = pre.next.next;
}
}
3.反转链表-力扣206
方法一:双指针
/**
* 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 cur = head;
ListNode pre = null;
while(cur != null){
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
方法二:递归
class Solution {
public static ListNode reverse(ListNode cur, ListNode pre){
if(cur == null) return pre;
ListNode temp = cur.next;
cur.next = pre;
return reverse(temp, cur);
}
public ListNode reverseList(ListNode head) {
return reverse(head, null);
}
}
方法三:建立虚拟头节点
class Solution {
public ListNode reverseList(ListNode head) {
//创建虚拟头节点
ListNode dummyhead = new ListNode(-1);
dummyhead.next = null;
//遍历所有节点
ListNode cur = head;
while(cur != null){
//采用头插法
ListNode temp = cur.next;
cur.next = dummyhead.next;
dummyhead.next = cur;
cur = temp;
}
return dummyhead.next;
}
}
方法四:使用栈
class Solution {
public ListNode reverseList(ListNode head) {
//如果链表为空,直接返回空
if(head == null) return null;
//如果链表只有一个元素
if(head.next == null) return head;
//建立一个栈
Stack<ListNode> stack = new Stack<>();
//入栈
ListNode cur = head;
while(cur != null){
stack.push(cur);
cur = cur.next;
}
//建立一个虚拟头节点,出栈
ListNode pHead = new ListNode(0);
cur = pHead;
while(!stack.isEmpty()){
ListNode node = stack.pop();
cur.next = node;
cur = cur.next;
}
//最后一个元素指向空
cur.next = null;
return pHead.next;
}
}
4.两两交换链表中的结点
/**
* 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 swapPairs(ListNode head) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode cur = dummyHead;
while(cur.next != null && cur.next.next != null){
ListNode temp = cur.next;
ListNode temp1 = cur.next.next.next;
cur.next = cur.next.next;
cur.next.next = temp;
temp.next = temp1;
cur = cur.next.next;
}
return dummyHead.next;
}
}
5.删除链表的倒数第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 removeNthFromEnd(ListNode head, int n) {
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
ListNode slow = dummyHead;
ListNode fast = dummyHead;
for(int i = 0; i <= n; i ++){
fast = fast.next;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummyHead.next;
}
}
6.力扣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) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
ListNode index1 = fast;
ListNode index2 = head;
while(index1 != index2){
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
}
标签:head,ListNode,cur,val,int,next,链表
From: https://www.cnblogs.com/wusuoweiju/p/17976470