我用String代替了链表显示,本题的大意是每k个进行逆序处理,剩下的不够k个的就按照原顺序保留下来. public class ReverseNodes { public static void main(String[] args) { String str = "1->2->3->4->5->6->7->8->9->10->11->12->13->14->15"; String[] strArra…
class Solution { public: ListNode *reverseKGroup(ListNode *head, int k) { if (!head || !(head->next) || k < 2) return head; // count k nodes ListNode *nextgp = head; for (int i = 0; i < k; i++) if (nextgp) nextgp = nextgp->next; else return he…
Total Accepted: 48614 Total Submissions: 185356 Difficulty: Hard 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 rem…
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra…
2.6Given a circular linked list,  implement an algorithm which returns the node at the beginning of the loop. 快指针和慢指针一起在头指针开始移动,快指针每次移动两步,慢指针每次移动一步,直到相遇, 相遇后,把慢指针指向头指针,两个指针一起往后移动一步.直到相遇,相遇的地方就是循环的开始(如果相遇,说明是有循环的节点,这时快指针走的路程是慢指针的两倍,快:开始的k距离,和一圈(或者n圈)循…
一.Reverse Linked List  (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked List /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class…
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 初想用递归来实现应该会挺好的,但最终运行时间有点久,达到72ms,虽然没超时.具体程序如下: /** * Definition for singly-linked list. *…
Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false Example 2: Input: 1->2->2->1 Output: true Follow up:Could you do it in O(n) time and O(1) space? Sloving with O(3n) -> O(n) Idea: reversing th…
翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致): 有了这个图(每次把博客发出去后就会发现图怎么变得这么小了哎!仅仅能麻烦大家放大看或者另存为了.这图命名是1400X600的).那么代码也就自然而然的出来了: ListNode* reverseList(ListNode* head) { ListNode* newHead = NULL; wh…
http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ #include <iostream> #include <vector> #include <algorithm> #include <queue> #include <stack> #include <string> #include <fstream> #include <m…