[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-0-en-105":3,"doc-seo-685718-105":59,"doc-detail-685718-en":130,"doc-similar-685718":154},{"code":4,"msg":5,"data":6},0,"success",[7,13,18,23,28,33,38,43,48,51,55],{"id":8,"doc_module":4,"doc_module_name":9,"category_name":10,"show_sort_weight":11,"slug":12},1,"Document","Story & Novel",90,"story-novel",{"id":14,"doc_module":4,"doc_module_name":9,"category_name":15,"show_sort_weight":16,"slug":17},2,"Literature",80,"literature",{"id":19,"doc_module":4,"doc_module_name":9,"category_name":20,"show_sort_weight":21,"slug":22},4,"Exam",70,"exam",{"id":24,"doc_module":4,"doc_module_name":9,"category_name":25,"show_sort_weight":26,"slug":27},5,"Comic",60,"comic",{"id":29,"doc_module":4,"doc_module_name":9,"category_name":30,"show_sort_weight":31,"slug":32},6,"Technology",50,"technology",{"id":34,"doc_module":4,"doc_module_name":9,"category_name":35,"show_sort_weight":36,"slug":37},7,"Healthcare",40,"healthcare",{"id":39,"doc_module":4,"doc_module_name":9,"category_name":40,"show_sort_weight":41,"slug":42},8,"Research & Report",30,"research-report",{"id":44,"doc_module":4,"doc_module_name":9,"category_name":45,"show_sort_weight":46,"slug":47},9,"Religion & Spirituality",20,"religion-spirituality",{"id":46,"doc_module":4,"doc_module_name":9,"category_name":49,"show_sort_weight":46,"slug":50},"World Cup","world-cup",{"id":52,"doc_module":4,"doc_module_name":9,"category_name":53,"show_sort_weight":52,"slug":54},10,"Lifestyle","lifestyle",{"id":56,"doc_module":4,"doc_module_name":9,"category_name":57,"show_sort_weight":24,"slug":58},19,"General","general",{"code":4,"msg":60,"data":61},"ok",{"site_id":62,"language":63,"slug":64,"title":65,"keywords":66,"description":67,"schema_data":68,"social_meta":123,"head_meta":125,"extra_data":127,"updated_unix":129},105,"en","14-linked-lists-tree-recursion-with-lists-linked-list-structure-and-processing","14-Linked-Lists - Tree Recursion with Lists - Linked List Structure and Processing","","Technical notes on linked lists and a recursive algorithm that finds the longest sublist whose elements sum to a target value. Covers viewing sequences as a first item plus the rest, using tree recursion to include or exclude the first element, and returning None when no solution exists. Defines a linked list as either empty or a first value with the rest, introduces a Link data class with typed recursion, and demonstrates safe processing by checking instance types before accessing first/rest. Includes example constructions and printing logic.",{"@graph":69,"@context":122},[70,84,105],{"@type":71,"itemListElement":72},"BreadcrumbList",[73,77,79,82],{"item":74,"name":75,"@type":76,"position":8},"https://docshare.wps.com","Home","ListItem",{"item":78,"name":9,"@type":76,"position":14},"https://docshare.wps.com/document/",{"item":80,"name":30,"@type":76,"position":81},"https://docshare.wps.com/document/technology/",3,{"item":83,"name":65,"@type":76,"position":19},"https://docshare.wps.com/document/14-linked-lists-tree-recursion-with-lists-linked-list-structure-and-processing/685718/",{"url":83,"name":65,"@type":85,"image":86,"author":91,"headline":65,"publisher":94,"fileFormat":97,"inLanguage":63,"description":67,"dateModified":98,"datePublished":99,"encodingFormat":97,"isAccessibleForFree":100,"interactionStatistic":101},"DigitalDocument",{"url":87,"@type":88,"width":89,"height":90},"https://docshare.wps.com/thumbnails/14-linked-lists-tree-recursion-with-lists-linked-list-structure-and-processing/685718.png","ImageObject",300,407,{"name":92,"@type":93},"putri.kecil17","Person",{"url":74,"name":95,"@type":96},"DocShare","Organization","application/pdf","2026-11-30","2026-11-07",true,{"@type":102,"interactionType":103,"userInteractionCount":19},"InteractionCounter",{"@type":104},"ViewAction",{"@type":106,"mainEntity":107},"FAQPage",[108,114,118],{"name":109,"@type":110,"acceptedAnswer":111},"What does the longest(s, n) function return and when does it return None?","Question",{"text":112,"@type":113},"It returns the longest sublist whose elements sum to n. If no such sublist exists, it returns None.","Answer",{"name":115,"@type":110,"acceptedAnswer":116},"How does tree recursion help search for the longest sublist?",{"text":117,"@type":113},"At each step, the algorithm tries including the first item and excluding it, then compares the resulting candidate lists to select the longer one.",{"name":119,"@type":110,"acceptedAnswer":120},"How should you safely access first and rest of a linked list?",{"text":121,"@type":113},"Check whether the value is an instance of Link before reading its first or rest; if it is not a Link, handle it as the empty linked list case.","https://schema.org",{"og:url":83,"og:type":124,"og:title":65,"og:site_name":95,"og:description":67},"article",{"robots":126,"canonical":83},"index,follow",{"doc_id":128,"site_id":62},685718,1794016971,{"code":4,"msg":5,"data":131},{"doc_id":128,"user_id":132,"nickname":92,"user_avatar":133,"doc_module":4,"category_id":29,"category_name":30,"doc_title":65,"doc_description":67,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":19,"is_deleted":4,"is_public":8,"is_downloadable":8,"audit_status":8,"page_count":139,"language":140,"language_code":63,"site_id":62,"html_lang":63,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":67,"update_tm":129,"read_time":144,"preview_page_count":14,"preview_urls":145},962094509873,"https://ap-avatar.wpscdn.com/davatar_3d24733baf745e90a7e4bdd5f77d97b2","Linked Lists  \nAnnouncements  \nTree Recursion with Lists  \n=  \ndef longest(s: list [ int ], n: int ) -> list [ int ] | None :  \nA list of integers or None  \n\"\"\"Return the longest sublist of s that sums to n, or None if none exists .  \n>>> longest([4, 1, 3, -1, 2, -1], 5)  \n[4, 1, -1, 2, -1]>>> longest([3, 1, 4], 6) None  \n\"\"\"  \nif len (s) == 0 :  \nif n == 0 :  \nreturn []  \nConcepts that are useful for this problem:  \n• A list can be viewed as a first item s[0] & the rest s[1:]  \n• Tree recursion is good for searching & counting variants  \n• To find a sublist, try including/excluding the first item  \n• Use isinstance (s, list ) to test whether s is a list  \n• The max function can pick the longer of two lists  \nelse :  \nreturn None  \nminus_first = longest(s[ 1 :],   ) without_first = longest(s[ 1 :], n)  \nif   :  \nwith_first =   minus_first if isinstance (without_first, list ):  \n[1, -1, 2, -1] is the longest way in s[1:] to make 1  \n[1, 3, -1, 2] is the longest way in s[1:] to make 5  \n[4, 1, 3, -1, 2] is a way to make 5  \nreturn   else :  \nreturn with_first else :  \nreturn without_first  \n4  \nViewing a Sequence as the First Item and the Rest   \ndef longest(s: list [ int ], n: int ) -> list [ int ] | None :  \nA list of integers or None  \n\"\"\"Return the longest sublist of s that sums to n, or None if none exists .  \n>>> longest([4, 1, 3, -1, 2, -1], 5)  \n[4, 1, -1, 2, -1]>>> longest([3, 1, 4], 6) None  \n\"\"\"  \nif len (s) == 0 :  \nif n == 0 :  \nreturn [] else :  \nConcepts that are useful for this problem:  \n• A list can be viewed as a first item s[0] & the rest s[1:]  \n• Tree recursion is good for searching & counting variants  \n• To find a sublist, try including/excluding the first item  \n• Use isinstance (s, list ) to test whether s is a list  \n• The max function can pick the longer of two lists  \nreturn None  \nminus_first = longest(s[ 1 :],  n- s [ 0 ]  ) without_first = longest(s[ 1 :], n) if  is instance (minus first , list )  :  \nwith_first =  [ s [ 0 ]] +  minus_first if isinstance (without_first, list ):  \n[1, -1, 2, -1] is the longest way in s[1:] to make 1  \n[1, 3, -1, 2] is the longest way in s[1:] to make 5  \n[4, 1, 3, -1, 2] is a way to make 5  \nreturn  max (with_first , without_first , key= len ) else :  \nreturn with_first else :  \nreturn without_first  \n5  \nLinked Lists  \nLinked List Structure   \nA linked list is either empty or a first value and the rest of the linked list  \nA linked list is a pair  \n3 4 5  \nWe will use an empty tupleas an empty linked list  \n| Link instance | Link instance | () |\n| --- | --- | --- |\n| first: 4 rest: \u003Cbr>\u003Cbr>| first: 5 rest: \u003Cbr>|  |\n\nThe first item isan attribute value  \nLink(3, Link(4, Link(5, ())))  \nOR  \nLink(3, Link(4, Link(5 )))  \nThe rest of the items are stored in a linked list  \nHow to draw it:  How to print it: (3 4 5)  \nLinked List Data Class   \nThe Link data class describes a non-empty LinkedList : one link in the chain  \ntype LinkedList[T] = Link[T] | tuple[()]  The type of an empty tuple   \n@dataclass  \nclass Link[T]:  \nfirst: T  \nrest: LinkedList[T] = () \\# rest defaults to ()  \nThe type variable T describes the type of the items contained in the linked list  \nA Link object is never empty: it has first and rest  \nThis is a Link[int] or a LinkedList[int](or a Link or a LinkedList )  \nLink(3, Link(4, Link(5 )))  \n(Demo)  \nLinked List Processing  \nHow to Work with Linked Lists   \nFor a LinkedList s, check if it's a Link before accessing its first and/or rest attributes  \nif isinstance (s, Link):  \n...  \nIn here you can access s.first and [s. rest](s. rest)  \nif not isinstance (s, Link):...  \nelse :  \nEach time you call Link on 2 arguments, the second operand must evaluate to a linked list!  \n• Link(4, () ) or Link(4, Link( . . . ) )  \n• Link(4, s ) as long as s is a name for a linked list  \n• Link(4, f( . . . ) ) as long as f returns a linked list  \nConstruct the rest of the linked list, then add to the front: 3  4  5 /   \n>>> s = ()  \n>>> s = Link(5 , s)  \n>>> s = Link(","cbCaibJAzjJihBaG","https://ap.wps.com/l/cbCaibJAzjJihBaG","pdf",151309,15,"English","# Tree Recursion with Lists\n## longest(s, n) recursion strategy\n# Viewing a Sequence as the First Item and the Rest\n# Linked Lists\n## Linked List Structure\n## Linked List Data Class\n# Linked List Processing\n## How to Work with Linked Lists\n## Constructing and printing a linked list","[{\"question\":\"What does the longest(s, n) function return and when does it return None?\",\"answer\":\"It returns the longest sublist whose elements sum to n. If no such sublist exists, it returns None.\"},{\"question\":\"How does tree recursion help search for the longest sublist?\",\"answer\":\"At each step, the algorithm tries including the first item and excluding it, then compares the resulting candidate lists to select the longer one.\"},{\"question\":\"How should you safely access first and rest of a linked list?\",\"answer\":\"Check whether the value is an instance of Link before reading its first or rest; if it is not a Link, handle it as the empty linked list case.\"}]","14-Linked-Lists - Tree Recursion with Lists - Linked List Structure and Processing | PDF",38,[146,150],{"page_no":8,"url":147,"width":148,"height":149},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/249c87ee5f10788f198b3b350281ca0e.webp",983,759,{"page_no":14,"url":151,"width":152,"height":153},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/b94c35e0a3f4261575e5b2893470e3c0.webp",982,758,{"list":155,"token":66},[156,162,168,174,180,187,193,200,206,212],{"doc_id":157,"doc_title":158,"file_type":137,"home_preview_img_url":159,"slug":160,"view_count":14,"category_name":30,"create_tm":161},685743,"12-Linked Lists - Algorithms: longest sublist sum to n","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000186244/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=05%2Ba7fdT0VGuxPBgdidxO1KOodA%3D","12-linked-lists-algorithms-longest-sublist-sum-to-n",1794017227,{"doc_id":163,"doc_title":164,"file_type":137,"home_preview_img_url":165,"slug":166,"view_count":24,"category_name":30,"create_tm":167},697945,"Linked Lists - Part 19 - Data Structures Lecture Notes","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230009214291/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=W5AEfHWc%2BmkAM8L0vEqYITWQxSU%3D","linked-lists-part-19-data-structures-lecture-notes",1794446271,{"doc_id":169,"doc_title":170,"file_type":137,"home_preview_img_url":171,"slug":172,"view_count":34,"category_name":30,"create_tm":173},697943,"Lecture 12 Doubly Linked Lists - (with Recursion)","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230009212198/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=5gJ9gcNDusRyLwP0Rl93K5qgcjs%3D","lecture-12-doubly-linked-lists-with-recursion",1794446071,{"doc_id":175,"doc_title":176,"file_type":137,"home_preview_img_url":177,"slug":178,"view_count":81,"category_name":30,"create_tm":179},732880,"Linked List Basics - Linked List Basics - Insertion, Deletion, Navigation, and Advanced Operations","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230031780344/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=9UZCICUvzV4UT%2BYVdzp%2Fp0fSr7c%3D","linked-list-basics-linked-list-basics-insertion-deletion-navigation-and-advanced-operations",1795382679,{"doc_id":181,"doc_title":182,"file_type":137,"home_preview_img_url":183,"slug":184,"view_count":185,"category_name":30,"create_tm":186},82767,"SkillFab: An Agent-Native Skill Production Platform","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229808908411/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=AkFoP6F6vr48LRJr9C2Rl0C%2FtvU%3D","skillfab-an-agent-native-skill-production-platform",25,1784182812,{"doc_id":188,"doc_title":189,"file_type":137,"home_preview_img_url":190,"slug":191,"view_count":44,"category_name":30,"create_tm":192},291156,"Programming from the Ground Up - Introductory Assembly and Programming Guide","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229903755637/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=EbyDOpxAcRSksjudRXDvO02min0%3D","programming-from-the-ground-up-introductory-assembly-and-programming-guide",1789643566,{"doc_id":194,"doc_title":195,"file_type":137,"home_preview_img_url":196,"slug":197,"view_count":198,"category_name":30,"create_tm":199},82579,"AutoRestTest at the SBFT 2026 Tool Competition","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229808901785/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=3AH9z0rXPFNfUqMJH0bdWsejHZs%3D","autoresttest-at-the-sbft-2026-tool-competition",22,1784181619,{"doc_id":201,"doc_title":202,"file_type":137,"home_preview_img_url":203,"slug":204,"view_count":19,"category_name":30,"create_tm":205},703152,"Apex Tutorial - Salesforce Apex Programming Basics","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230013420991/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=%2ByBUrVMDpQ1e0M07dYDhpzYdsNM%3D","apex-tutorial-salesforce-apex-programming-basics",1794561718,{"doc_id":207,"doc_title":208,"file_type":137,"home_preview_img_url":209,"slug":210,"view_count":24,"category_name":30,"create_tm":211},685741,"ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY - Linked list implementation - Singly linked lists - Core algorithms for traversal, counting, searching and insertion","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000186201/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=GSIEDRqdb%2BtSurRUiT7TPHJ67u4%3D","rohini-college-of-engineering-technology-linked-list-implementation-singly-linked-lists-core-algorithms-for-traversal-counting-searching-and-insertion",1794017223,{"doc_id":213,"doc_title":214,"file_type":137,"home_preview_img_url":215,"slug":216,"view_count":34,"category_name":30,"create_tm":217},685760,"CS62 Class 8 - Linked lists - Basic Data Structures","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000192614/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111699&Signature=1rcjVsRDOgWaeg3Kb%2F3HcqKMQWE%3D","cs62-class-8-linked-lists-basic-data-structures",1794017629]