ListNode* Solution::subtract(ListNode* A) { if (!A || !A->next) { return A; } // Find mid and end of linkedList ListNode* mid = A; ListNode* end = A; while (end->next) { end = end->next; if (end->next) { end = end->next; mid = mid->next; } } // Reverse second half of the linked list mid->next = reverseLinkedList(mid->next); // Calculate the new values of first half of the linked list ListNode* startNode = A; ListNode* endNode = mid->next; while(endNode) { startNode->val = endNode->val - startNode->val; startNode = startNode->next; endNode = endNode->next; } // Reverse second half of linked list again mid->next = reverseLinkedList(mid->next); return A; }
int main(int argc, char **argv) { struct Node *head = initializeList(); head = reverseLinkedList(head); printLinkedList(head); return 0; }
int main(){ insertLinkedListNode(30); insertLinkedListNode(40); insertLinkedListNode(50); printLinkedListNodes(); reverseLinkedList(); printLinkedListNodes(); return 0; }
struct node * reverseLinkedList(struct node *head) { if (head == NULL || head->next == NULL) return head; struct node * temp = reverseLinkedList(head->next); head->next->next = head; head->next = NULL; return temp; }
ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode dummy(-1); dummy.next = head; ListNode* prev = &dummy; for(int i = 1; i < m; ++i) { prev = prev -> next; } reverseLinkedList(prev, m, n); return dummy.next; }
int main() { List l = makeList(10); PrintList(l); std::cout << "------" << std::endl; l = reverseLinkedList(l); PrintList(l); return 0; }
struct node * reverseLinkedList(struct node *head) { struct node *ptrback; if (head == NULL || head->next == NULL) { return head; } ptrback = reverseLinkedList(head->next); head->next->next = head; head->next = NULL; return ptrback; }
bool isPalindrome(ListNode* head) { ListNode* tempHead = head; int length = 0; for (; tempHead; tempHead = tempHead->next) ++length; ListNode* mid = head; for (int i = 0; i < length / 2; i++, mid = mid->next); if (length % 2) mid = mid->next; mid = reverseLinkedList(mid); tempHead = head; ListNode* tempMid = mid; for (int i = 0; i < length / 2; i++, tempHead = tempHead->next, tempMid = tempMid->next) { if (tempHead->val != tempMid->val) { mid = reverseLinkedList(mid); return false; } } mid = reverseLinkedList(mid); return true; }
bool isPalindrome(const LinkedList& list) { LinkedList reversed = reverseLinkedList(list); Node* reversedNode = reversed.getHead(); Node* node = list.getHead(); while(node != nullptr && reversedNode != nullptr) { if(node->getValue() != reversedNode->getValue()) return false; node = node->getNext(); reversedNode = reversedNode->getNext(); } return true; }
struct node * reverseLinkedList(struct node *head) { if (head){ struct node * temp; if (head == NULL || head->next == NULL) { return head; } temp = reverseLinkedList(head->next); head->next->next = head; head->next = NULL; return temp; } else{ return NULL; } }
struct node * reverseLinkedList(struct node *head) { if (head == NULL) return NULL; struct node *root = NULL; if (head->next != NULL) { root = reverseLinkedList(head->next); head->next->next = head; head->next = NULL; return root; } else{ return head; } return head; }
int main(int argc, char const *argv[]) { LinkedList l = LinkedList(0); l.append(2).append(1).append(2).append(1).append(2).append(0); l.print(); LinkedList rev = reverseLinkedList(l); rev.print(); std::cout << "list is "; if(!isPalindrome(l)) std::cout << "not "; std::cout << "palindrome." << std::endl; return 0; }
/** * Main function, * Allocates a LinkedList with 10 items, prints it to show the order, * calls reverseLinkedList to reverse it, and prints it again to see * if the order indeed was reversed. **/ void main (int argc, char *argv[]) { int i; int len = 10; if (argc > 1) { len = atoi(argv[1]); } Item *List = NULL; for (i=0; i<len; i++) { Item *item = malloc(sizeof(Item)); item->v = len - i; addToLinkedList(&List, item); } printLinkedList(List); Item *revList = reverseLinkedList(List); printLinkedList(revList); }