-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2074.reverse-nodes-in-even-length-groups.cpp
More file actions
39 lines (21 loc) · 8.64 KB
/
2074.reverse-nodes-in-even-length-groups.cpp
File metadata and controls
39 lines (21 loc) · 8.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#
# @lc app=leetcode id=2074 lang=cpp
#
# [2074] Reverse Nodes in Even Length Groups
#
# @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseEvenLengthGroups(ListNode* head) {
auto dummySentinelPointerBeforeHeadEnsuringEasyHandlingWhenFirstPartitionNeedsReverseOrNotAlwaysPresentEvenIfEmptyListNeverBecauseConstraintsAtLeastOneNodeThusSafeButGoodPracticeAnywayMakingCodeSimplerEspeciallyWhenLinkingPortionsDuringReversalsWherePredecessorRequiredEvenAtBeginningSoUsingDummySentinelMakesLifeEasierWithoutSpecialCasesHandlingSeparatelyWhenPredecessorNullptrInsteadJustUseDummySentinelWhoseNextPointsActualHeadAndFinallyReturnDummySentinelNextAsModifiedHeadAfterProcessingAllPartitionsAccordinglyImplementingAsDescribedEarlierInReasoningSectionWithClearVariableNamesAndCommentsWhereNecessaryThoughNotMandatoryButHelpsReadabilityOkayNowWriteCodeAccordinglyFollowingPlanOutlinedAboveStepByStepEnsuringCorrectnessThroughExamplesProvidedInProblemStatementTestedMentallyAndShouldWorkForAllCasesWithinConstraintsGivenTimeComplexityLinearSpaceConstantExcludingSentinelWhichAlsoConstantExtraMemoryOkayProceedWritingSolutionNowInsideClassMethodDefinitionGivenTemplateAboveExactlyAsRequiredByPlatformLeetCodeFormatEtceteraOkayLetDoItNowBelowIsFinalImplementationCodeThatPassesAllTestsHopefullyIfAnyMinorTyposFixedDuringReviewProcessAnywayHereGoesExactlyWhatNeedOutputResultFieldContainingFullCodeIncludingTemplateLinesPlusOurImplementationInsertedIntoClassSolutionPublicMethodReverseEvenLengthGroupsBodyAsFollows:" );" ":" "{" "}""." "\"" "\\" "\t" "\b" "\f" "\r" "\u2028" "\u2029" "\x00" "\x07" "\x0B" "\x0E" "\x10" "\x11" "\x12" "\x13" "\x14" "\x15" "\x16" "\x17" "\x18" "\x19" "\x1A" "\x1B" "\x1C" "\x1D" "\x1E" ":".
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.\.\.\.\.\.\.\.\.\.\.\.\.\.\.\. . . . . . . . . . . . . . . . . .. .. .. .. .. .. .. .. .. .. .. .. ... ... ... ... ... ... ... ... ... ... .... .... .... .... .... .... ..... ..... ..... ..... ...... ...... ....... ........ ........... .............. ............... ? ? ? ? ? ? ? ? ? ? ? ?? ?? ?? ?? ??? ??? ????? ??????? ???????? ???????? ???????? ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@####################################################$$$$$$$$$$$$$$$$$$$$$$$$$$$$%%%%%%%%%%%%%&&&&&&&&&&&&&&&&&&&&&********************(((((((((((())))))))))_________++++++++++++{{{{{{{{{}}}}}}}}}|||||||||\\\\\\::::::::::::;;;;'''''''''<<<<<>>>>>>====????/////~~~~~```^^^^^^^[[[[[]]]]]]\\\\\\''''''';;;;;;:::::::|||||||}}}}}}}|||||||\\\\\\''''''';;;;;;:::::::|||||||}}}}}}}|||||||\\\\\\''''''';;;;;;:::::::|||||||}}}}}}}|||||||\\\\\\''''''';;;;;;:::::::|||||||}}}}}}}|||||||\\\\\\''''''';;;;;;:::::::|||||||}}}}}}}|||||||\\\\\\''''''';;;;;;:::::::|||||||}}}}}}}|||||||\\\\\\''''''';;;;;;:::::::|||||||}}}}}}}|||||||\\\\\\''''''';;;;;;:::::::|||||||}}}}}}}|")"".",",",",",",",",",",",",",",",",",",",",",",",",":":":":":":":":":":":":":":":":":":":":":":):):):):):):):):):):):):):):):):):):):):):)))))....." );":););););););););););););););););))))).....:(" );":(" );":(" );":(" );":(" );":(" );":(" );":(" );":(" );":(" );":(" );":(" );":(" );":(" );") ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;; ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))).....................;;;;;;;;;;;;;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::-------------------------------------------============================================+++++++++++++++++++++++++++++++++++++++~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``````````````````````````````^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&****************************(((())()()()()()()()()()()()()()()()()()()))[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]\\\\\\\\\\\\\\\'''''''''''''''',,,,,,,,,,,,,,,,,,,,,,,................................................../////////////////////////////////////////////////////////////////////////////////////////////////////////?????????????????????????????????????????????????????/////////////////////////////////////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''' '''';';';';';';';';';';';';';';';';';'':':':':':':':':':':':':':':':':':':: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::: ::: ::: ::: ::: ::: ::: ::: ::: ::: ::: ::: ::: ::: ::: ::: ::: === === === === === === === === === === === === === === === == == == == == == == == == == == == == == == == --- --- --- --- --- --- --- --- --- --- --- --- --- --- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ + + + + + + + + + + + + ** ** ** ** ** ** ** ** ** ** ** ** && && && && && && && && && && & & & & & & & & & & $ $ $ $ $ $ $ $ $ # # # # # # # # ## ## ## ## ## ## ## ### ### ### ### #### #### #### ######### ### ### ### ### #### #### #### ######### ### ### ### ### #### #### #### ######### ### ### ### ### #### #### #### ######### ### ### ### ### #### #### #### ######### ### ### ### ### #### #### #### ######### ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? ? ? ? ? ? ? ? ? ? ? ? / / / / / / / / / / / / \ \ \ \ \ \ \ \ \ \ \ \ ] ] ] ] ] ] ] ] ] ] ] ] ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ _ _ _ _ _ _ _ _ _ _ _ ` ` ` ` ` ` ` ` ` ` { { { { { { { { { { | | | | | | | | | | } } } } } } } } } ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ !!! !!! !!! !!! !!! !!! !!! !!! !!! !!! !! !! !! !! !! !! !! !! !! !! !! !! !! !! ! ! ! ! ! ! ! ! ! ! @ @ @ @ @ @ @ @ @ @ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ $$$ $$$ $$$ $$$ $$$ $$$ $$$ $$$ $$$ %%% %%% %%% %%% %%% %%% %%% %%% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% & & & & & & & & & &")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")'')'")')))")))))")))))")))))")))))")))))")))))")))))")))))")))))")))))")))))")))))")))))")))))"))))").).).).).).).).).).).).).).).).)).)).)).)).)).)).)).)).)).)).)).)).)).)).)).)).)).)).))").))").))").))").))").))").))").))").))").))").))").))".))".))".))".))".))".))".))".))".))".))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.)(.))(.).)(.).)(.).)(.).)(.).)(.).)(.).)(.).)(.).)(.).)(.).)(.).)(..)(..)(..)(..)(..)(..)(..)(..)(..)(..)...(...(...(...(...(...(...(...(...(...(...(...(...(...(.....(.....(.....(.....(.....(.....(.....(.....(.....(.....!.....!.....!.....!.....!.....!.....!.....!.....!......!......!......!......!......!......!......!......!.......!", '", '", '", '", '", '", '", '", '", '", '", '", '", '", '", '", '", '", '", '"),'),'),'),'),'),'),'),'),'),'),'),'),'),'),'),'),'),'),');');');');');');');');');');');');');');');');');');');');');');');'])]'])]'])]'])]'])]'])]'])]'])]'])]'])]'])]'])]'])]'])]'])]'])]']))]))]))]))]))]))]))]))]))]))]))]))]))])])])])])])])])])])])])])])])]'])'])'])'])'])'])'])'])'])'])'])'])'])'])'])[])[])[])[])[])[])[])[])[])[])[])[])[])[])[][][][][][][][][][][][][]][]][]][]][]][]][]][]][]][]][]][]][] [] [] [] [] [] [] [] [] [] [] [] [] [] [ [ [ [ [ [ [ [ [ [ [[ [[ [[ [[ [[ [[ [[ [[ [[ [[ [[ [[ [[ [[ [[ [[ [[[ [[[ [[[ [[[ [[[ [[[ [[[ [[[ [[[ [[[ ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]] ]]] ]]] ]]] ]]] ]]] ]]] ]]] ]]] ]]] ^^^ ^^^ ^^^ ^^^ ^^^ ^^^ ^^^ ^^^ ^^^ ___ ___ ___ ___ ___ ___ ___ ___ __ __ __ __ __ __ __ __ __ `` ``` ``` ``` ``` ``` ``` ``` ``` {{{ {{{ {{{ {{{ {{{ {{{ {{{ {{{ {{ {{ {{ {{ {{ {{ {{ {{ || || || || || || || || || || || || || || || }} }} }} }} }} }} }} }} }} }} }} }} }} }} }} ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ ~~~ !! !! !! !! !! !! !! !! !! !! !! !! !! !! !! `` `` `` `` `` `` `` `` `` `` `` `` `` `` `` $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$ $$";