leetcode笔记—翻转链表

1、翻转链表

 void reverseNodes(ListNode *start, ListNode *end) {   //翻转链表
     ListNode *second = start -> next;
     ListNode *first = start;
     ListNode *temp;

     while(second != end) {
         temp = second -> next;
         second -> next = first;
         first = second;
         second = temp;
     }

     second -> next = first;

    }     //翻转后start指向最后一个节点

2、链表相邻的k个节点翻转

void reverseNodes(ListNode *start, ListNode *end) {
     ListNode *second = start -> next;
     ListNode *first = start;
     ListNode *temp;

     while(second != end) {
         temp = second -> next;
         second -> next = first;
         first = second;
         second = temp;
     }

     second -> next = first;
 }

 ListNode *reverseKGroup(ListNode *head, int k) {
     ListNode *tempHead = head, *tempEnd = head;
     ListNode *result = head;
     ListNode *preEnd = head;
     ListNode *temp = NULL;
     bool flag = true;
     int count = 1;

     if(!head) {
         return NULL;
     }

     while(tempEnd -> next) {
         tempEnd = tempEnd -> next;
         count++;

         if(count == k) {
             temp = tempEnd -> next;
             if(flag) {
                 flag = false;
                 result = tempEnd;
                 reverseNodes(tempHead, tempEnd);
                 tempHead -> next = temp;
                 count = 1;
             }  //翻转第一个k个节点;
             else {
                 preEnd -> next = tempEnd;
                 preEnd = tempHead;
                 reverseNodes(tempHead, tempEnd);
                 tempHead -> next = temp;
                 count = 1;
             }//翻转后面的节点

             if(temp) {
                 tempHead = temp;
                 tempEnd = temp;
             }
             else {
                 tempHead -> next = NULL;
                 return result;
             }
         }
     }
     return result;
 }
};

//以上程序是有问题的,不知道哪里错了,下面是个accepted

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        ListNode** re=&head;
        ListNode* pre=head;
        ListNode* q=NULL;
        while(true)
        {
            int i=k;
            ListNode* temp=pre;
            while(temp!=NULL&&--i>0)
            {
                temp=temp->next;
            }     //取到K个节点的尾
            if(temp==NULL) return head;//如果没有K长度,返回
            i=k;
            while(i--)
            {    //翻转
                ListNode *p_next=pre->next;
                pre->next=q;
                q=pre;
                pre=p_next;
            }
            (*re)->next=pre;
            ListNode *t=*re;
            *re=q;
            re=&(t->next);
            q=NULL;

        }
        return head;
    }
};
时间: 2024-07-30 17:11:31

leetcode笔记—翻转链表的相关文章

[LeetCode系列]翻转链表问题II

给定一个链表和两个整数m, n, 翻转链表第m个节点到第n个节点(从1开始计数). 如, 给定链表: 1->2->3->4->5->NULL, 以及 m = 2, n = 4. 返回 1->4->3->2->5->NULL. 假定m和n满足约束条件: 1 ≤ m ≤ n ≤ 链表长度. 注意: 不能使用额外空间, 且只能遍历链表一次. 算法思路: 翻转的过程可以分解成3步: 把相邻的节点的指向关系倒置; 即 1->2<-3<-4

(Java) LeetCode 25. Reverse Nodes in k-Group —— k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

[LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

[LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod

[leetcode] 25. k个一组翻转链表

25. k个一组翻转链表 仍然是链表处理问题,略微复杂一点,边界条件得想清楚,画画图就会比较明确了. reverse函数表示从 front.next节点开始,一共k个节点做反转. 即: 1>2>3>4>5 ,k = 2.当front为1时, 执行reverse后: 1>3>2>4>5 同上个题一样,申请一个空的(无用的)头指针指向第一个节点,会方便许多. public ListNode reverseKGroup(ListNode head, int k)

[LeetCode]25. Reverse Nodes in k-Group k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in

[LeetCode] 25. K 个一组翻转链表 ☆☆☆☆☆(链表)

https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/javadi-gui-fang-fa-100-by-chadriy-imdgvs6udp/ https://leetcode-cn.com/problems/reverse-nodes-in-k-group/solution/tu-jie-kge-yi-zu-fan-zhuan-lian-biao-by-user7208t/ 描述 给你一个链表,每 k 个节点一组

leetcode笔记

leetcode 笔记 Linked List 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a

[LintCode] Reverse Nodes in k-Group 每k个一组翻转链表

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, only nod