链表的定义
public class ListNode {
int val;
ListNode next;
public ListNode(){}
public ListNode(int val){
this.val=val;
}
public ListNode(int val,ListNode next){
this.val =val;
this.next=next;
}
}
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
例题1 删除某个值
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
pre=ListNode(0)
pre.next=head
cur=pre
while cur.next:
if cur.next.val==val:
cur.next=cur.next.next
else :cur=cur.next
return pre.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; }
* }
*/
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode pre= new ListNode(0,head);
ListNode cur=pre;
while(cur.next!= null){
if (cur.next.val==val){
cur.next=cur.next.next;
}else{
cur=cur.next;
}
}
return pre.next;
}
}
例题2 翻转链表
题目链接: link
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev=None
while head:
tmp=head.next
head.next=prev
prev=head
head=tmp
return prev
/**
* 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 prev = null;
while (head != null){
ListNode tmp=head.next;
head.next=prev;
prev=head;
head=tmp;
}
return prev;
}
}
例题3 翻转两个结点
https://leetcode.cn/problems/swap-nodes-in-pairs/
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
prev=ListNode(0)
prev.next=head
cur=prev
while cur.next and cur.next.next:
tmp=cur.next
cur.next=cur.next.next
tmp.next=tmp.next.next
cur.next.next=tmp
cur=cur.next.next
return prev.next
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head or not head.next:
return head
newhead=head.next
head.next=self.swapPairs(newhead.next)
newhead.next=head
return newhead
/**
* 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 prev = new ListNode(0,head);
ListNode cur=prev;
while (cur.next !=null && cur.next.next != null){
ListNode tmp1=cur.next;
ListNode tmp2=cur.next.next;
cur.next=tmp2;
tmp1.next=tmp2.next;
tmp2.next=tmp1;
cur=tmp1;
}
return prev.next;
}
}
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode newhead=head.next;
head.next = swapPairs(newhead.next);
newhead.next=head;
return newhead;
}
}
例题4 删除链表的倒数第 N 个结点
https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
cur=ListNode(0)
cur.next=head
fast,slow=cur,cur
for i in range(n):
fast=fast.next
while fast and fast.next:
fast=fast.next
slow=slow.next
slow.next=slow.next.next
return cur.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; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode cur= new ListNode(0,head);
ListNode fast=cur;
ListNode slow=cur;
for(int i=0;i<n;i++){
fast=fast.next;
}
while (fast != null && fast.next != null){
fast=fast.next;
slow=slow.next;
}
slow.next=slow.next.next;
return cur.next;
}
}