算法通关村第二关白银挑战——两两交换链表中的节点

两两交换链表中的节点

我的思路:

def swapPairs(self, head):
    if not head or not head.next:
        return head
    prehead=ListNode(-1)
    prehead.next=head
    cur,pre=head,prehead
    while cur and cur.next:
        next=cur.next
        cur.next=next.next
        next.next=cur
        pre.next=next
        pre=cur
        cur=cur.next
    return prehead.next

单链表加一

使用链表反转

我的思路:

因为在链表反转的题目中,以下就想到了链表反转

难点:

1.最后一个数如果为9,要进行拆分,这个特殊情况没有考虑到,拆分后的两个数排列也需要仔细考虑,因为还要反转链表。然后是拆分后会多出来一个,我第一次忘记直接跳出去了,拆分的前面那个数也加了一,悲伤

2.进位符号要记得每次清零啊

def plusOne(self,head):
    def reverist(head):
        if not head or not head.next:
            return head
        cur = head.next
        head.next = None
        while cur:
            next = cur.next
            cur.next = head
            head = cur
            cur = next
        return head

    head = reverist(head)
    ans = 0
    cur = head
    cur.val = cur.val + 1
    while cur:
        if ans == 1:
            cur.val = cur.val + 1
            ans=0
        if cur.val >= 10:
            if cur.next==None:
                next=ListNode(cur.val%10)
                cur.val=cur.val/10
                cur.next=next
                break
            else:
                ans = 1
                cur.val = cur.val % 10
        cur = cur.next
    head = reverist(head)
    return head

使用栈

难点:循环不太好,写出来了却因为range取值不对错误了

代码太繁琐了,需要改进

def plusOne(self,head):
    if not head :
        return head
    cur=head
    pre=ListNode(0)
    pre.next=head
    s=[]
    len=0
    ans=1
    add=0
    while cur:
        s.append(cur.val)
        cur=cur.next
        len+=1
    cur = pre.next
    for i in range(len-1,-1,-1):
        if ans==1:
            s[i]+=1
            ans=0
        if s[i]>=10:
            if i==0:
                add=1
                s[i]=0
            else:
                s[i]=0
                ans=1
    for i in range(0,len):
        cur.val=s[i]
        cur=cur.next
    if add==1:
        adder=ListNode(1)
        adder.next=pre.next
        pre.next=adder
    return pre.next

老师的方法——找9法

思路:找到最右边的不是9的数字,这是进位终止的地方

注意:必须设置虚拟结点为0,这样才可以把特殊情况也解决

 
def plusOne(self,head):
    ans=ListNode(0)
    ans.next=head
    not_nine=ans

    while head:
        if head.val!=9
            not_nine=head
        head=head.next

    not_nine.val+=1
    not_nine=not_nine.next

    while not_nine:
        not_nine.val=0
        not_nine=not_nine.next

    return ans if ans.val else ans.next

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是交换链表节点的C语言代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct Node { int data; struct Node* next; } Node; // 定义交换链表节点的函数 void swapNodes(Node** head, int x, int y) { // 如果x和y相同,直接返回 if (x == y) { return; } // 定义需要交换节点和它们的前驱节点 Node *prevX = NULL, *currX = *head; Node *prevY = NULL, *currY = *head; // 寻找需要交换节点和它们的前驱节点 while (currX != NULL && currX->data != x) { prevX = currX; currX = currX->next; } while (currY != NULL && currY->data != y) { prevY = currY; currY = currY->next; } // 如果x或y不存在于链表,直接返回 if (currX == NULL || currY == NULL) { return; } // 如果x不是头节点,更新前驱节点的next指针 if (prevX != NULL) { prevX->next = currY; } else { // 如果x是头节点,更新头指针 *head = currY; } // 如果y不是头节点,更新前驱节点的next指针 if (prevY != NULL) { prevY->next = currX; } else { // 如果y是头节点,更新头指针 *head = currX; } // 交换节点的next指针 Node* temp = currY->next; currY->next = currX->next; currX->next = temp; } // 定义输出链表的函数 void printList(Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { // 创建一个测试用例链表 Node* head = (Node*)malloc(sizeof(Node)); head->data = 1; head->next = (Node*)malloc(sizeof(Node)); head->next->data = 2; head->next->next = (Node*)malloc(sizeof(Node)); head->next->next->data = 3; head->next->next->next = (Node*)malloc(sizeof(Node)); head->next->next->next->data = 4; head->next->next->next->next = NULL; // 输出原始链表 printf("原始链表: "); printList(head); printf("\n"); // 交换节点 swapNodes(&head, 2, 4); // 输出交换后的链表 printf("交换后的链表: "); printList(head); printf("\n"); return 0; } ``` 以上代码将创建一个测试用例链表,输出原始链表交换节点,输出交换后的链表。可以根据自己的需要修改测试用例链表
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值