【数据结构与算法】leetcode刷题记录(合并两个有序链表+删除链表的倒数第n个元素) --->链表练习

合并两个有序链表

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的

示例:

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

java

递归

/**
 * 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 mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null)   return l2;
        if(l2==null)    return l1;
        if(l1.val<=l2.val){
            l1.next = mergeTwoLists(l1.next,l2);
            return l1;
        }
        l2.next = mergeTwoLists(l1,l2.next);
        return l2;
    }
}

非递归

建立一个节点

dummp = [ 0 : next-]>null

l1 = [3,next-]->[5,next-]->[7,next-]->null

l2 = [2,next-]->[4,next-]->[6,next-]->null

我们每次循环只需要比较 l1 和 l2 的头节点即可: 如果 l1.val 小于 l2.val , 那么就把 l1 的头节点添加到 dummp中, 反之就把 l2 的头节点添加到dummp中 , 然后对应的链表的next后移;

//具体过程
dummp = [0:next-]>null
l1 = [3,next-]->[5,next-]->[7,next-]->null
l2 = [2,next-]->[4,next-]->[6,next-]->null
//很明显
    l2.val = 2 < l1.val = 3;
//所以, dummp.next = l2 ; l2 = l2.next
dummp = [0:next-]->[2,next-]->null
l1 = [3,next-]->[5,next-]->[7,next-]->null
l2 = [4,next-]->[6,next-]->null
//在比较,
    l1.val = 3 < l2.val = 4;
//所以, dummp.next = l1 ; l1 = l1.next
dummp = [0:next-]->[2,next-]->[3,next-]->null
l1 = [5,next-]->[7,next-]->null
l2 = [4,next-]->[6,next-]->null
//循环到l1和l2中一个为空.....    
dummp = [0:next-]->[2,next-]->[3,next-]->[4,next-]->[5,next-]->[6,next-]->null
l1 = [7,next-]->null
l2 = null
//这时应该把不为null的那个链表添加到dummp.next
	l1 != null   
dummp = [0:next-]->[2,next-]->[3,next-]->[4,next-]->[5,next-]->[6,next-]-> [7,next-]->null
l1 = null
l2 = null 
//然后
	return dummp.next即可 //return dummp会把开头的第一个0输出出来
/**
 * 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 mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null)return l2;
        if(l2==null)return l1;
        ListNode dummp = new ListNode(0);
        ListNode p = dummp;
        while(l1!=null&&l2!=null){
            if(l1.val<l2.val){
                p.next = l1;
                l1 = l1.next;
            }else{
                p.next = l2;
                l2 = l2.next;
            }
            p = p.next;
        }
        p.next = l1==null ? l2:l1;
        return dummp.next;
    }
}

python

非递归 思路同上

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
   def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
       if l1 == None:
           return l2
       if l2 == None:
           return l1
       dummp = ListNode(0)
       p = dummp
       while l1!=None and l2!=None:
           if l1.val<l2.val:
               p.next = l1
               l1 = l1.next
           else:
               p.next = l2
               l2 = l2.next
           p = p.next
       if l1 == None:
           p.next = l2
       else:
           p.next = l1
       return dummp.next

删除链表的倒数第n个元素

给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。

示例:

给定一个链表: 1->2->3->4->5, 和 n = 2.

当删除了倒数第二个节点后,链表变为 1->2->3->5.
说明:

给定的 n 保证是有效的。

python

栈+遍历链表解决

建立dummp节点,另dummp的下一节点为head

先遍历一遍链表,然后把所有节点都添加进栈中

然后再对栈进行弹出

弹出n次

那么第n次后,所得元素就是我们要找的位置,

把这个位置的元素用pre保存

执行pre = pre.next

返回dummp.next即可

# 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: ListNode, n: int) -> ListNode:
        dummp = ListNode(0,head)
        p = dummp
        stack = []
        while p:
            stack.append(p)
            p = p.next
        while(n>0):
            stack.pop()
            n-=1
        pre = stack[-1]
        pre.next=pre.next.next
        return dummp.next

java

特殊:使用deque作为栈

Deque stack = new LinkedList();

stack.push和stack.pop用来添加和弹出

/**
 * 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 dummp = new ListNode(0,head);
        ListNode p = dummp;
        Deque<ListNode> stack = new LinkedList<ListNode>();
        while(p!=null){
            stack.push(p);
            p = p.next;
        }
        while(n>0){
            stack.pop();
            n--;
        }
        ListNode pre = stack.peek();;
        pre.next = pre.next.next;
        return dummp.next;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值