[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"doc-seo-685743-105":3,"detail-sidebar-cat-0-en-105":80,"doc-detail-685743-en":130,"doc-similar-685743":154},{"code":4,"msg":5,"data":6},0,"ok",{"site_id":7,"language":8,"slug":9,"title":10,"keywords":11,"description":12,"schema_data":13,"social_meta":73,"head_meta":75,"extra_data":77,"updated_unix":79},105,"en","12-linked-lists-algorithms-longest-sublist-sum-to-n","12-Linked Lists - Algorithms: longest sublist sum to n","","Materials focus on using linked lists as a recursive data structure and applying them to a specific algorithmic task: finding the longest contiguous sublist of an integer list whose elements sum to a target n. It explains representing a list as a first item plus the rest, employing tree recursion with include/exclude branching, and selecting between candidate results by length. The document also provides a linked-list structure using tuples and a Link dataclass, plus practical rules for constructing and processing linked lists safely.",{"@graph":14,"@context":72},[15,34,55],{"@type":16,"itemListElement":17},"BreadcrumbList",[18,23,27,31],{"item":19,"name":20,"@type":21,"position":22},"https://docshare.wps.com","Home","ListItem",1,{"item":24,"name":25,"@type":21,"position":26},"https://docshare.wps.com/document/","Document",2,{"item":28,"name":29,"@type":21,"position":30},"https://docshare.wps.com/document/technology/","Technology",3,{"item":32,"name":10,"@type":21,"position":33},"https://docshare.wps.com/document/12-linked-lists-algorithms-longest-sublist-sum-to-n/685743/",4,{"url":32,"name":10,"@type":35,"image":36,"author":41,"headline":10,"publisher":44,"fileFormat":47,"inLanguage":8,"description":12,"dateModified":48,"datePublished":49,"encodingFormat":47,"isAccessibleForFree":50,"interactionStatistic":51},"DigitalDocument",{"url":37,"@type":38,"width":39,"height":40},"https://docshare.wps.com/thumbnails/12-linked-lists-algorithms-longest-sublist-sum-to-n/685743.png","ImageObject",300,407,{"name":42,"@type":43},"Noah","Person",{"url":19,"name":45,"@type":46},"DocShare","Organization","application/pdf","2026-11-12","2026-11-07",true,{"@type":52,"interactionType":53,"userInteractionCount":26},"InteractionCounter",{"@type":54},"ViewAction",{"@type":56,"mainEntity":57},"FAQPage",[58,64,68],{"name":59,"@type":60,"acceptedAnswer":61},"How is a linked list represented in this material?","Question",{"text":62,"@type":63},"A linked list is either empty or a first value with the rest of the linked list. The empty list is modeled as an empty tuple, and non-empty lists are represented using a Link structure (with first and rest fields).","Answer",{"name":65,"@type":60,"acceptedAnswer":66},"How does the algorithm find the longest sublist that sums to n?",{"text":67,"@type":63},"It uses tree recursion by viewing the list as first item plus the rest. For each step, it tries two options: include the first item (reducing the target to n - s[0]) or exclude it (keeping the target as n), then chooses the longer result by length.",{"name":69,"@type":60,"acceptedAnswer":70},"What checks are recommended before accessing linked-list fields?",{"text":71,"@type":63},"Processing should verify whether the value is a Link instance using isinstance before reading its first and rest attributes. If it is not a Link, the value is treated as the empty linked list case.","https://schema.org",{"og:url":32,"og:type":74,"og:title":10,"og:site_name":45,"og:description":12},"article",{"robots":76,"canonical":32},"index,follow",{"doc_id":78,"site_id":7},685743,1794017227,{"code":4,"msg":81,"data":82},"success",[83,87,91,95,100,104,109,114,119,122,126],{"id":22,"doc_module":4,"doc_module_name":25,"category_name":84,"show_sort_weight":85,"slug":86},"Story & Novel",90,"story-novel",{"id":26,"doc_module":4,"doc_module_name":25,"category_name":88,"show_sort_weight":89,"slug":90},"Literature",80,"literature",{"id":33,"doc_module":4,"doc_module_name":25,"category_name":92,"show_sort_weight":93,"slug":94},"Exam",70,"exam",{"id":96,"doc_module":4,"doc_module_name":25,"category_name":97,"show_sort_weight":98,"slug":99},5,"Comic",60,"comic",{"id":101,"doc_module":4,"doc_module_name":25,"category_name":29,"show_sort_weight":102,"slug":103},6,50,"technology",{"id":105,"doc_module":4,"doc_module_name":25,"category_name":106,"show_sort_weight":107,"slug":108},7,"Healthcare",40,"healthcare",{"id":110,"doc_module":4,"doc_module_name":25,"category_name":111,"show_sort_weight":112,"slug":113},8,"Research & Report",30,"research-report",{"id":115,"doc_module":4,"doc_module_name":25,"category_name":116,"show_sort_weight":117,"slug":118},9,"Religion & Spirituality",20,"religion-spirituality",{"id":117,"doc_module":4,"doc_module_name":25,"category_name":120,"show_sort_weight":117,"slug":121},"World Cup","world-cup",{"id":123,"doc_module":4,"doc_module_name":25,"category_name":124,"show_sort_weight":123,"slug":125},10,"Lifestyle","lifestyle",{"id":127,"doc_module":4,"doc_module_name":25,"category_name":128,"show_sort_weight":96,"slug":129},19,"General","general",{"code":4,"msg":81,"data":131},{"doc_id":78,"user_id":132,"nickname":42,"user_avatar":133,"doc_module":4,"category_id":101,"category_name":29,"doc_title":10,"doc_description":12,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":26,"is_deleted":4,"is_public":22,"is_downloadable":22,"audit_status":22,"page_count":139,"language":140,"language_code":8,"site_id":7,"html_lang":8,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":12,"update_tm":79,"read_time":144,"preview_page_count":26,"preview_urls":145},8796095462418,"https://ap-avatar.wpscdn.com/avatar/80000253c1241d02b47?x-image-process=image/resize,m_fixed,w_180,h_180&k=1778826106357471780","Linked Lists  \nAnnouncements  \nTree Recursion with Lists  \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 :  \nreturn None  \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  \nminus_first = longest(s[ 1 :],   ) without_first = longest(s[ 1 :], n)  \nif   :  \nwith_first =   minus_first  \nif isinstance (without_first, list ):  \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, -1, 2, -1] 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:  What it looks like printed: (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(4 , s)  \n>>> s = Link(3 , s)  \n>>> print(s)(3 4 5)  \nLink(3 , Link(4 , Link(5)))","cbCaifW7n7h0BDHL","https://ap.wps.com/l/cbCaifW7n7h0BDHL","pdf",177375,17,"English","# Tree Recursion with Lists\n# Viewing a Sequence as the First Item and the Rest\n# Linked List Structure\n# Linked List Data Class\n# Linked List Processing\n## How to Work with Linked Lists","[{\"question\":\"How is a linked list represented in this material?\",\"answer\":\"A linked list is either empty or a first value with the rest of the linked list. The empty list is modeled as an empty tuple, and non-empty lists are represented using a Link structure (with first and rest fields).\"},{\"question\":\"How does the algorithm find the longest sublist that sums to n?\",\"answer\":\"It uses tree recursion by viewing the list as first item plus the rest. For each step, it tries two options: include the first item (reducing the target to n - s[0]) or exclude it (keeping the target as n), then chooses the longer result by length.\"},{\"question\":\"What checks are recommended before accessing linked-list fields?\",\"answer\":\"Processing should verify whether the value is a Link instance using isinstance before reading its first and rest attributes. If it is not a Link, the value is treated as the empty linked list case.\"}]","12-Linked Lists - Algorithms: longest sublist sum to n | PDF",43,[146,150],{"page_no":22,"url":147,"width":148,"height":149},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/0c37b3470f64da4b60e23cb39d7ec407.webp",983,759,{"page_no":26,"url":151,"width":152,"height":153},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-09/5c479291bb872ef9873474dc29900ab6.webp",982,758,{"list":155,"token":11},[156,162,168,174,180,186,192,198,204,210],{"doc_id":157,"doc_title":158,"file_type":137,"home_preview_img_url":159,"slug":160,"view_count":33,"category_name":29,"create_tm":161},685718,"14-Linked-Lists - Tree Recursion with Lists - Linked List Structure and Processing","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000182459/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111459&Signature=CzA2FGxX4w%2FrX59yP4TOUgA74Qs%3D","14-linked-lists-tree-recursion-with-lists-linked-list-structure-and-processing",1794016971,{"doc_id":163,"doc_title":164,"file_type":137,"home_preview_img_url":165,"slug":166,"view_count":96,"category_name":29,"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=1796111459&Signature=XlXLycISu87fgpHMnYrB59gbaDs%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":105,"category_name":29,"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=1796111459&Signature=%2BxnWRzDrptRtbMg1TWYVDoheQy8%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":105,"category_name":29,"create_tm":179},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=1796111459&Signature=PE7lJ90Y3e5msSF6LYdWSSN7Y5o%3D","cs62-class-8-linked-lists-basic-data-structures",1794017629,{"doc_id":181,"doc_title":182,"file_type":137,"home_preview_img_url":183,"slug":184,"view_count":96,"category_name":29,"create_tm":185},685782,"1.4.2 - Data Structures - Advanced Notes - Specification","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000208907/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111459&Signature=9slq7XE8P6poIFtZSFCDMFIN%2B8U%3D","142-data-structures-advanced-notes-specification",1794018468,{"doc_id":187,"doc_title":188,"file_type":137,"home_preview_img_url":189,"slug":190,"view_count":30,"category_name":29,"create_tm":191},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=1796111459&Signature=tjLKovJCOo6am%2Fl4PpjqC5Mzv4Y%3D","linked-list-basics-linked-list-basics-insertion-deletion-navigation-and-advanced-operations",1795382679,{"doc_id":193,"doc_title":194,"file_type":137,"home_preview_img_url":195,"slug":196,"view_count":96,"category_name":29,"create_tm":197},685762,"Linked List - Course 13004700 Part-2","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229999691129/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111459&Signature=Jv8M4DJVhP08bdrZ%2FI5Z5U%2BpHqM%3D","linked-list-course-13004700-part-2",1794017631,{"doc_id":199,"doc_title":200,"file_type":137,"home_preview_img_url":201,"slug":202,"view_count":33,"category_name":29,"create_tm":203},685674,"Lecture 18 - Linked Lists","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229999686601/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111459&Signature=au%2BPzOQeDaEEmVLRgclTaei3N6c%3D","lecture-18-linked-lists",1794016551,{"doc_id":205,"doc_title":206,"file_type":137,"home_preview_img_url":207,"slug":208,"view_count":96,"category_name":29,"create_tm":209},685779,"Topic 6 - Lists, Sets, Tuples, Dictionaries","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203959/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111459&Signature=7q5%2BN5zVFWF%2F2JH4ESu4lnWPQis%3D","topic-6-lists-sets-tuples-dictionaries",1794018231,{"doc_id":211,"doc_title":212,"file_type":137,"home_preview_img_url":213,"slug":214,"view_count":30,"category_name":29,"create_tm":215},685724,"LINKED LISTS - Chapter 3 - overview","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000182527/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796111459&Signature=JepdCV2NACNnUfUrMb9vRTZ9bFo%3D","linked-lists-chapter-3-overview",1794016986]