链表学习(java、python)

链表的定义

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;

    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值