Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use leetcode to delete linked list repeating elements in golang

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use leetcode in golang to delete linked list repetitive elements, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Delete the repeating elements in the sorted list

Given a sorted linked list, delete all duplicate elements so that each element appears only once.

Example 1:

Enter: 1-> 1-> 2

Output: 1-> 2

Example 2:

Enter: 1-> 1-> 2-> 3-> 3

Output: 1-> 2-> 3

Problem-solving idea: because it is a sorted linked list, it can be solved directly according to the traversal train of thought.

1, if cur.Val==next.Val,cur.Next=next.Next

2, the linked list can be traversed normally

/ * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode * * / func deleteDuplicates (head * ListNode) * ListNode {cur:=head if cur==nil {return cur} next:=cur.Next for next! = nil {fmt.Println (cur,next) if cur.Val==next.Val {next=next.Next cur.Next=next fmt.Println (cur) Next)} else {cur=next next=next.Next}} return head}

Delete duplicate elements in the linked list (no sorting)

Given an unordered linked list, delete all nodes that contain duplicate numbers.

Example 1:

Enter: 1-> 3-> 2-> 3-> 5-> 4-> 4

Output: 1-> 3-> 2-> 5-> 4

Example 2:

Enter: 1-> 1-> 1-> 2-> 3

Output: 1-> 2-> 3

In this case, you need to hash the element value, and ignore the element if it does not exist.

/ * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode * * / func deleteDuplicates (head * ListNode) * ListNode {h:=&ListNode {} h.Next=head cur:=head m:=make (mapping [int] int) next:=cur.Next for curvilinear cursors nil & & if m [next.Val] + if m [next.Val] = 0 {cur=next next=next.Next} else {cur.Next=next.Next next=next.Next}} if NextDetecnil & & m [next.Val] = = 0 {cur.Next=next} else {cur.Next=nil} return h.Next}

Delete the repeating element II in the sorted list

Given a sorted linked list, delete all nodes that contain duplicate numbers, leaving only those numbers that are not repeated in the original linked list.

Example 1:

Enter: 1-> 2-> 3-> 3-> 4-> 4-> 5

Output: 1-> 2-> 5

Example 2:

Enter: 1-> 1-> 1-> 2-> 3

Output: 2-> 3

Ideas for solving the problem:

1. Always put pre in front of cur and determine whether the repeating element exists by judging cur.val = = cur.next.val.

2. For simplicity, add a header to the linked list

H-> 1-> 2 334-> 4-> 5

| | |

Pre cur

/ * Definition for singly-linked list. * type ListNode struct {* Val int * Next * ListNode * * / func deleteDuplicates (head * ListNode) * ListNode {h:=&ListNode {} h.Next=head cur:=head pre:=h for curvilinear cursornil {flag:=false for cur.NextFortunnil & & cur.Val==cur.Next.Val {cur=cur.Next flag=true} if flag { Pre.Next=cur.Next} else {pre.Next=cur pre=cur} cur=cur.Next} return h.Next} above is how to delete the repeating elements of the linked list using leetcode in golang The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report